Django 通用视图的url参数
我有一个关于错误url的愚蠢问题 在html菜单中:Django 通用视图的url参数,django,url,url-parameters,django-generic-views,Django,Url,Url Parameters,Django Generic Views,我有一个关于错误url的愚蠢问题 在html菜单中: <a href="/user_app/update_profile?pk={{ user.pk }}">Update my profile</a> 在view.py中: class UpdateProfile(UpdateView): form_class = UpdateProfileForm template_name = "user_app/update_profile.html" #
<a href="/user_app/update_profile?pk={{ user.pk }}">Update my profile</a>
在view.py中:
class UpdateProfile(UpdateView):
form_class = UpdateProfileForm
template_name = "user_app/update_profile.html"
#add a success message if the form is valid
def form_valid(self, form):
messages.success(self.request, "Your profile has been updated.", extra_tags='success')
return super(UpdateProfile, self).form_valid(form)
#modifcation: display the user's data given his primary key
def get_object(self):
return UserModel.objects.get(pk=self.request.GET.get('pk'))
在我的模板update_profile.html中:
<form action="." method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
为什么我的url在提交时被更改
编辑:奇怪的错误…
如果我使用模板上下文处理器获取当前url,我可以在模板中使用完整的当前url:
<form action="{{ current_path }}" method="post">
当我显示它时,我得到当前路径=“/user\u app/update\u profile?pk=1”
现在,当我提交表单时,我被重定向到“/users/admin/”并得到一个错误(我的网站中没有这样的url)。此url来自何处?编辑:已解决强>
我刚刚将get_success_url添加到我的视图中,现在它可以工作了:):
尝试从模板中删除
action=“.”
。非常奇怪。。。如果我删除表单操作,提交后的url为“users/admin/”。我的网站上没有这样的url!尝试表单中的action=“”。我遇到了相同的问题:我被重定向到/users/admin/。我不知道这个url是从哪里来的……您是否在您的模型上定义了一个get\u absolute\u url
?
Using the URLconf defined in arpaso.urls, Django tried these URL patterns, in this order:
^/?$
^auth/?$
^user_app/? ^show_profile
^user_app/? ^update_profile
The current URL, user_app/, didn't match any of these.
<form action="{{ current_path }}" method="post">
def get_success_url(self):
succ_url = '/user_app/update_profile?pk='+self.request.GET.get('pk')
return succ_url