我之前遇到了NoReverse Match错误,我盲目地猜测了它。并且找不到页面我一直都能修复,所以我真的不确定发生了什么。
据我所知,一切都应该正常工作,我甚至将此代码与之前的项目进行了比较,看起来很好(我一直在修补它,所以它可能不是我最初输入的内容)。< / p>
base.html文件
from django.shortcuts import render
from users.forms import UserForm,UserProfileForm
from users.models import UserProfileInfo
from django.contrib.auth import authenticate,login,logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import (TemplateView,ListView,
DetailView,CreateView,
UpdateView,DeleteView)
# Create your views here.
def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username,password=password)
if user:
if user.is_active:
login(request,user)
return HttpResponseRedirect(reverse('index'))
else:
return HttpResponse("Account now active")
else:
print("Login Unsuccessful")
return HttpResponse("Username and/or password are not correct")
else:
return render(request,'login.html',{})
def register(request):
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
profile_form = UserProfileForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES['profile_pic']
profile.save()
registered = True
else:
print(user_form.errors,profile_form.errors)
else:
user_form = UserForm()
profile_form = UserProfileForm()
return render(request,'register.html',{
'user_form':user_form,
'profile_form':profile_form,
'registered':registered
})
@login_required
def user_logout(request):
logout(request)
return HttpResponseRedirect(reverse('login'))
class HomeView(ListView):
model = UserProfileInfo
ordering = ['-join_date']
class UserProfileView(DetailView):
model = UserProfileInfo
class UserEditProfileView(LoginRequiredMixin,UpdateView):
login_url = '/login/'
redirect_field_name = '/users_detail.html'
form_class = UserProfileForm
model = UserProfileInfo
app views.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from users import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$',views.HomeView.as_view(),name='index'),
url(r'^users/',include('users.urls',namespace='users')),
]
项目urls.py
from django.conf.urls import url
from users import views
app_name = 'users'
urlpatterns = [
url(r'^login/$',views.user_login,name='user_login'),
url(r'^logout/$',views.user_logout,name='user_logout',kwargs={'next_page':'/'}),
url(r'^register/$',views.register,name='register'),
url(r'(?P<username>\d+)/$',views.UserProfileView.as_view(),name='user-profile'),
url(r'(?P<username>\d+)/edit$',views.UserEditProfileView.as_view(),name='user-profile-edit'),
]
app urls.py
curl_easy_perform
文件结构:
答案 0 :(得分:0)
正如您在urls.py
中所看到的那样:
url(r'(?P<username>\d+)/$',views.UserProfileView.as_view(),name='user-profile')
您的user-profile
网址带有一个参数,即username
。但是在你的模板中你没有通过它:
<a class="nav-link" href="{% url 'users:user-profile' %}">New Color Set</a>
因此Django不知道要传递哪个用户名,这会导致NoReverseMatch
。
您只需要将username
传递给网址:
<a class="nav-link" href="{% url 'users:user-profile' username=user.username %}">New Color Set</a>
答案 1 :(得分:0)
想出来:问题出现了,因为在另一个模板中(我没有发布,因为我没有意识到它对此有任何影响)我没有放入命名空间a
这是其他模板:
users
感谢大家的帮助。