代码之家  ›  专栏  ›  技术社区  ›  LeLouch

NoReverseMatch at/

  •  0
  • LeLouch  · 技术社区  · 8 年前

    因此,我试图制作一个简单的更新配置文件应用程序,但当我尝试访问更新页面时,我必须手动编写链接+在本例中的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>
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Daniel Roseman    8 年前

    正如错误所说,edit_user需要一个参数:要编辑的用户的id。你需要在 url 标签

    {% url 'edit_user' pk=my_user.pk %}
    

    或者不管用户对象的名称是什么。