【问题标题】:My user profile update view (class based view) returns a 404我的用户配置文件更新视图(基于类的视图)返回 404
【发布时间】:2016-01-15 16:18:52
【问题描述】:

我有一个 UserProfile 模型,我想让人们根据需要进行更新。

views.py

from django.views.generic.edit import CreateView, UpdateView

class UserProfileUpdateView(UpdateView):
    model = UserProfile
    form_class = UserProfileForm
    template_name = "preferences.html"
    success_url = "/profile/"

    def get_context_data(self, *args, **kwargs):
        context = super(UserProfileUpdateView, self).get_context_data(*args, **kwargs)
        return context

urls.py

from profiles import views
from profiles.views import UserProfileCreateView, UserProfileUpdateView

urlpatterns = [url(r'^profile/(?P<pk>\d+)$/edit', UserProfileUpdateView.as_view(), name='update_profile'),]

现在我的问题是,当我尝试转到 http://127.0.0.1:8000/profile/someusername/edit 时,我得到一个 404。我不明白 urlpatterns 中的 pk 到底发生了什么。我应该使用什么 url 模式来查看可以更新个人资料的页面?还是这里有什么问题?

【问题讨论】:

  • 另外,您的 URL 模式需要一个 id,看起来您正在发送用户名,但这是行不通的。更改正则表达式模式以接受字符

标签: python django django-class-based-views


【解决方案1】:

您的正则表达式中间有一个$,这是没有意义的,因为那是终止符。删除它。

【讨论】:

  • 或者更好的是,将它移到正则表达式的末尾:^profile/(?P&lt;pk&gt;\d+)/edit$
  • 非常感谢,它结合了正则表达式和@karthikr 所说的内容。
猜你喜欢
  • 1970-01-01
  • 2021-05-03
  • 2016-03-03
  • 2022-11-20
  • 2020-01-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-02
  • 1970-01-01
相关资源
最近更新 更多