Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图在Django中查看用户配置文件,但在搜索URL时找不到页面_Python_Django_Django Views_Django Class Based Views_User Profile - Fatal编程技术网

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

Python 试图在Django中查看用户配置文件,但在搜索URL时找不到页面,python,django,django-views,django-class-based-views,user-profile,Python,Django,Django Views,Django Class Based Views,User Profile,我试图查看我的网站的用户配置文件的详细信息CBV。下面是views.py、url.py、models.py和profile.html的代码 存在用户名为“brian_weber”的用户,但由于某种原因,当我导航到此链接时:找不到该页面。我的应用程序名为“账户” 如果有人能给我指出正确的方向,让视图显示在url中,那将是非常感激的!我已经搜索了堆栈溢出,但我所尝试的都没有效果 提前感谢, 布莱恩 视图.py from django.shortcuts import render from djan

我试图查看我的网站的用户配置文件的详细信息CBV。下面是views.py、url.py、models.py和profile.html的代码

存在用户名为“brian_weber”的用户,但由于某种原因,当我导航到此链接时:找不到该页面。我的应用程序名为“账户”

如果有人能给我指出正确的方向,让视图显示在url中,那将是非常感激的!我已经搜索了堆栈溢出,但我所尝试的都没有效果

提前感谢,

布莱恩

视图.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
url.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"),
]
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[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"),
]
username\u regex=r'[a-zA-Z0-9\+'
URL模式=[
url(r“^logout/$”,views.logout\u view,name=“logout”),
url(r“^signup/$”,views.signup.as_view(),name=“signup”),
url(r'^profile/(?P{username})$)。格式(username=username\u regex),
views.UserProfile.as_view(),
name=“profile”),
url(r“^profile/update/(?P{username})$”。格式(username=username\u regex),
views.UserProfileUpdate.as_view(),
name=“更新_配置文件”),
]

您的错误很小,url中为概要文件提供的正则表达式,
(?p[a-zA-Z0-9]+)
不匹配
brian_weber
,因为
\ucode>

您也可以简单地更新正则表达式以匹配

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"),
]
username\u regex=r'[a-zA-Z0-9\+'
URL模式=[
url(r“^logout/$”,views.logout\u view,name=“logout”),
url(r“^signup/$”,views.signup.as_view(),name=“signup”),
url(r'^profile/(?P{username})$)。格式(username=username\u regex),
views.UserProfile.as_view(),
name=“profile”),
url(r“^profile/update/(?P{username})$”。格式(username=username\u regex),
views.UserProfileUpdate.as_view(),
name=“更新_配置文件”),
]

@e4c5您拥有17k的声誉,少回答一个问题不会伤害您:-DThanks MJafar!现在我有另一个错误,这次是一个值错误。该错误对于以10为基数的int()无效:“brian_weber”。我必须重新定义get_对象方法吗?如果是这样,代码会是什么样子?@Brian是的,你可以这样做。另一个解决方案是设置
slug\u字段='user\u username'
,虽然我还没有测试它,但我猜它是基于这个链接工作的:我使用了slug\u url\u kwarg='username'和slug\u字段='user\u username'。现在我的观点有效了!非常感谢:)@e4c5您拥有17k的声誉,少回答一个问题不会伤害您:-DThanks MJafar!现在我有另一个错误,这次是一个值错误。该错误对于以10为基数的int()无效:“brian_weber”。我必须重新定义get_对象方法吗?如果是这样,代码会是什么样子?@Brian是的,你可以这样做。另一个解决方案是设置
slug\u字段='user\u username'
,虽然我还没有测试它,但我猜它是基于这个链接工作的:我使用了slug\u url\u kwarg='username'和slug\u字段='user\u username'。现在我的观点有效了!非常感谢:)