Когда пользователь нажимает кнопку «Войти» Я хочу, чтобы войти в систему, и перенаправляется т.е., www.exampledomain.com/accounts/usernameGoesHere/Получение ошибки NoReverseMatch при попытке добавить имя пользователя в URL после авторизации
Вот мой URLs уровня:
from django.conf.urls import url, include
from django.contrib import admin
# Namespace URLs
app_name = "pto_request"
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('accounts.urls')),
]
и здесь является URLs для счетов:
from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from accounts.views import (login_view, register_view, logout_view)
from . import views
app_name = 'accounts'
urlpatterns = [
# root url will look like www.website.com/accounts/
url(r'^login/$', login_view, name='login'),
url(r'^logout/$', logout_view, name='logout'),
url(r'^register/$', register_view, name='register'),
url(r'^(?P<username>[0-9a-zA-Z._]+)/$', login_required(views.IndexView.as_view()), name = 'index'),
]
Это мое мнение Логина:
def login_view(request):
title = "Login"
user_form = UserLoginForm(request.POST or None)
if user_form.is_valid():
username = user_form.cleaned_data.get('username')
password = user_form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
login(request, user)
return redirect(reverse('accounts:index', args=[username]))
return render(request, 'form.html', {'user_form':user_form, 'title':title})
и, наконец, это журнал отслеживающий ошибки я получаю:
Environment:
Request Method: GET
Request URL: http://localhost:8000/accounts/FlashBanistan/
Django Version: 1.10.5
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'datetimewidget',
'accounts']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware']
Template error:
In template C:\django projects\PTO\accounts\templates\accounts\index.html, error at line 0
Reverse for 'index' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['accounts/(?P<username>[0-9a-zA-Z._]+)/$'] 1 : {% extends 'base.html' %}
2 : {% load crispy_forms_tags %}
3 :
4 : {% block content %}
5 :
6 : {{ form.media }}
7 :
8 :
9 : <div class="container">
10 : <div id="calendar">
Traceback:
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in _get_response
217. response = self.process_exception_by_middleware(e, request)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in _get_response
215. response = response.render()
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\response.py" in render
109. self.content = self.rendered_content
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\response.py" in rendered_content
86. content = template.render(context, self._request)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\backends\django.py" in render
66. return self.template.render(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render
208. return self._render(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render
994. bit = node.render_annotated(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render_annotated
961. return self.render(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\loader_tags.py" in render
174. return compiled_parent._render(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in _render
199. return self.nodelist.render(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render
994. bit = node.render_annotated(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render_annotated
961. return self.render(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\defaulttags.py" in render
315. return nodelist.render(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render
994. bit = node.render_annotated(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render_annotated
961. return self.render(context)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\defaulttags.py" in render
439. url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\urls\base.py" in reverse
91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\Users\achesley\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
392. (lookup_view_s, args, kwargs, len(patterns), patterns)
Exception Type: NoReverseMatch at /accounts/FlashBanistan/
Exception Value: Reverse for 'index' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['accounts/(?P<username>[0-9a-zA-Z._]+)/$']
Вы можете не "перенаправлять() их из login_view? – abolotnov
Почему вы не можете просто вернуть перенаправление («учетные записи /% s»% username) из login_view? – abolotnov
ваш код не выглядит безопасным, кстати; в момент, когда вы выполните login(), пользователь может быть None. – abolotnov