所以我试图创建一个简单的更新配置文件应用程序但是当我尝试访问更新页面时我必须手动编写链接+在这种情况下的ID是2例如'accounts / update / 2'否则它将不会当我尝试使用“{%url'edit_user'%}时,我的工作
`NoReverseMatch at /
Reverse for 'edit_user' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['accounts/update/(?P<pk>\\d+)/']`
url.py
url(r'^admin/', admin.site.urls),
url(r'^$', main_views.home, name='home'),
url(r'^uprofile$', main_views.uprofile, name='uprofile'),
url(r'^accounts/update/(?P<pk>\d+)/', User_Profile_views.edit_user, name='edit_user'),
url(r'^accounts/', include('allauth.urls')),
views.py
@login_required() # only logged in users should access this
def edit_user(request, pk):
# querying the User object with pk from url
user = User.objects.get(pk=pk)
# prepopulate UserProfileForm with retrieved user values from above.
user_form = UserForm(instance=user)
# The sorcery begins from here, see explanation below
ProfileInlineFormset = inlineformset_factory(User, UserProfile, fields=('website', 'bio', 'phone', 'city', 'country', 'organization'))
formset = ProfileInlineFormset(instance=user)
if request.user.is_authenticated() and request.user.id == user.id:
if request.method == "POST":
user_form = UserForm(request.POST, request.FILES, instance=user)
formset = ProfileInlineFormset(request.POST, request.FILES, instance=user)
if user_form.is_valid():
created_user = user_form.save(commit=False)
formset = ProfileInlineFormset(request.POST, request.FILES, instance=created_user)
if formset.is_valid():
created_user.save()
formset.save()
return HttpResponseRedirect('/accounts/profile/')
return render(request, "account/account_update.html", {
"noodle": pk,
"noodle_form": user_form,
"formset": formset,
})
else:
raise PermissionDenied
html表单
<div class="col s12 m8 offset-m2">
<div class="card">
<div class="card-content">
<h2 class="flow-text">Update your information</h2>
<form action="." method="POST" class="padding">
{% csrf_token %} {{ noodle_form.as_p }}
<div class="divider"></div>
{{ formset.management_form }}
{{ formset.as_p }}
<button type="submit" class="btn-floating btn-large waves-light waves-effect"><i class="large material-icons">done</i></button>
<a href="#" onclick="window.history.back(); return false;" title="Cancel" class="btn-floating waves-effect waves-light red"><i class="material-icons">history</i></a>
</form>
</div>
</div>
</div>
答案 0 :(得分:1)
如错误所示,edit_user需要一个参数:要编辑的用户的ID。您需要在url
标记中传递它。
{% url 'edit_user' pk=my_user.pk %}
或用户对象的任何名称。