我正在尝试使用Detail CBV查看我的网站的用户个人资料。以下是views.py,urls.py,models.py和profile.html的代码。
存在用户名为“brian_weber”的用户,但出于某种原因,当我导航到此链接时:http://0.0.0.0:8000/accounts/profile/brian_weber找不到该页面。我的应用程序称为“帐户”。
如果有人能指出我正确的方向让视图出现在网址上,那将非常感激!我搜索过Stack Overflow,但没有尝试过。
提前致谢,
布赖恩
views.py
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.contrib.auth import login, logout, authenticate
from django.http import HttpResponseRedirect
from django.contrib.auth.forms import AuthenticationForm
from django.core.urlresolvers import reverse, reverse_lazy
from django.views import generic
from braces.views import LoginRequiredMixin
from django.contrib.messages.views import SuccessMessageMixin
from . import forms
from . import models
class LoginView(generic.FormView):
form_class = AuthenticationForm
success_url = reverse_lazy('home')
template_name = "accounts/login.html"
def get_form(self, form_class=None):
if form_class is None:
form_class = self.get_form_class()
return form_class(self.request, **self.get_form_kwargs())
def form_valid(self, form):
login(self.request, form.get_user())
return super().form_valid(form)
def logout_view(request):
logout(request)
return HttpResponseRedirect(reverse('home'))
class SignUp(SuccessMessageMixin, generic.CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy("login")
template_name = "accounts/signup.html"
success_message = "Your profile has been successfully created. Please log into your account."
class UserProfile(LoginRequiredMixin, generic.DetailView):
model = models.UserProfile
template_name = "profile.html"
class UserProfileUpdate(LoginRequiredMixin, generic.UpdateView):
model = models.UserProfile
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^logout/$', views.logout_view, name="logout"),
url(r'^signup/$', views.SignUp.as_view(), name="signup"),
url(r'^profile/(?P<username>[a-zA-Z0-9]+)$',
views.UserProfile.as_view(),
name="profile"),
url(r'^profile/update/(?P<username>[a-zA-Z0-9]+)$',
views.UserProfileUpdate.as_view(),
name="update_profile"),
]
models.py
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
company = models.CharField(max_length=40, null=True)
position = models.CharField(max_length=40, null=True)
bio = models.CharField(max_length=140, blank=True, default="")
avatar = models.ImageField(blank=True, null=True, upload_to="avatars",
height_field=None, width_field=None)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
答案 0 :(得分:2)
由于(?P<username>[a-zA-Z0-9]+)
,您的错误非常小,个人资料网址中提供的正则表达式brian_weber
与_
不匹配。
您只需更新正则表达式即可与_
匹配。
username_regex = r'[a-zA-Z0-9_]+'
urlpatterns = [
url(r'^logout/$', views.logout_view, name="logout"),
url(r'^signup/$', views.SignUp.as_view(), name="signup"),
url(r'^profile/(?P<username>{username})$'.format(username=username_regex),
views.UserProfile.as_view(),
name="profile"),
url(r'^profile/update/(?P<username>{username})$'.format(username=username_regex),
views.UserProfileUpdate.as_view(),
name="update_profile"),
]