尝试在 Django 中查看用户配置文件,但在搜索 URL 时找不到页面

Trying to view User Profile in Django, but Page Not Found when searching URLs

我正在尝试使用 Detail CBV 查看我网站的用户配置文件。下面是 views.py、urls.py、models.py 和 profile.html.

的代码

存在一个用户名为 "brian_weber" 的用户,但由于某种原因,当我导航到此 link 时:http://0.0.0.0:8000/accounts/profile/brian_weber 找不到该页面。我的应用名为 "accounts"。

如果有人能给我指出正确的方向,让视图与 url 一起显示,那将不胜感激!我搜索了 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)

你的错误很小,url中提供的配置文件正则表达式,(?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"),
]