Reverse not found: context for sending a request from templates

NB This question was significantly modified before the first answer was given.

Hello,

I am new to django, so I apologize if I am missing something obvious.

I have a urls.py file that looks like this:

urlpatterns = patterns(
    '',
    (r'^$', 'faros.lantern.views.home_page'),
    (r'^login/$', 'django.contrib.auth.views.login'),
    (r'^logout/$', 'django.contrib.auth.views.logout'),
    (r'^about/$', 'faros.lantern.views.about_page_index', {}, 'about_page_index'),
    (r'^about/(?P<page_id>([a-z0-9]+/)?)$', 'faros.lantern.views.about_page', {}, 'about_page'),
)

Views that look like this:

def about_page_index(request):
    try:
        return render_to_response('lantern/about/index.html', context_instance=RequestContext(request))
    except TemplateDoesNotExist:
        raise Http404

def about_page(request, page_id):
    page_id = page_id.strip('/ ')

    try:
        return render_to_response('lantern/about/' + page_id + '.html', context_instance=RequestContext(request))
    except TemplateDoesNotExist:
        raise Http404

And a template that includes the following:

<a href="{% url lantern.views.about_page_index %}">Contact</a>
<a href="{% url lantern.views.about_page page_id="contact" %}">Contact</a>

I get this error message:

Caught an exception while rendering: Reverse for '<function about_page at 0x015EE730>' with arguments '()' and keyword arguments '{'page_id': u'contact'}' not found. The first reverse works fine (about_page_index), generating the correct URL without error messages.

I think this is because the request argument is used in the about_page ( request) view , so I need to pass it when I create the url in my template. The problem is that I don’t know how to get to it, and the search around me will not deliver. Any ideas?

Thanks,

House

p.s. , "" ? URL- , - , about/. , , , , . / , !

+3
1

(def about_page(request, page_id = None):), , , URL-, , page_id. , django , , , page_id. !: -)

, url (. urlpatterns). :

(r'^about/(?P<page_id>([a-z]+/)?)$', 'faros.lantern.views.about_page', 
 {}, 'about_with_page_id')

:

<a href="{% url about_with_page_id page_id="contact" %}">Contact</a>

Edit

urls.py. url ( , lantern.views:

<a href="{% url about_page_index %}">Contact</a>
<a href="{% url about_page page_id="contact" %}">Contact</a>

Edit2

, . , django , , . :

r'^about/(?P<page_id>([a-z]+/)?)$'

:

r'^about/(?P<page_id>[a-z0-9]+)/$'

, , . , !: -)

+9

Source: https://habr.com/ru/post/1706344/


All Articles