NoReverseMatch - 如何将url参数添加到渲染?

时间:2016-09-11 01:13:23

标签: django django-views django-urls

url或任何其他解决方案的情况下,是否有任何方法可以提供render参数?不知何故,我需要提供user_url

# view
def create_gallery(request, user_url):

    if request.method == 'POST':
        extended_form = GalleryExtendedForm(request.POST, prefix="extended_form")
        basic_form = GalleryForm(request.POST, prefix="basic_form")
        print(request.POST)
        if extended_form.is_valid() and basic_form.is_valid():
            pass
    else:
        extended_form = GalleryExtendedForm(prefix="extended_form")
        basic_form = GalleryForm(prefix="basic_form")

        return render(request, 'galleries/gallery_create_n_update.html', 
                     {'extended_form': extended_form,
                      'basic_form': basic_form})

# core.urls
urlpatterns = [
    url(r'^(?P<user_url>[\w.-]+)/', include('profiles.urls', namespace='profiles_user')),
]

# profiles.urls
urlpatterns = [
    url(r'^gallery/', include('galleries.urls')),
    # ... lots of others urls
]

# galleries.urls
urlpatterns = [
    url(r'^add/$', views.create_gallery, name='gallery-add'),
]

错误:

django.urls.exceptions.NoReverseMatch: Reverse for 'gallery-add' with arguments '('',)'
and keyword arguments '{}' not found.
1 pattern(s) tried: ['(?P<user_url>[\\w.-]+)/gallery/add/$']

1 个答案:

答案 0 :(得分:1)

您应该将user_url传递到context的{​​{1}}参数,以便可以在模板中使用。

然后,在模板中,您可以将render()作为参数添加到url template tag,如下所示:

user_url