Я столкнулся с этой проблемой при создании страницы в tango_with_django_project.

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/rango/category//add_page/
Using the URLconf defined in tango_with_django_project.urls, Django tried these URL patterns, in this order:
admin/
^rango/ ^$ [name='index']
^rango/ about/$ [name='about']
^rango/ ^add_category/$ [name='add_category']
^rango/ ^category/(?P<category_name_url>\w+)/$ [name='category']
^rango/ ^category/(?P<category_name_url>\w+)$/add_page/$ [name='add_page']
^media\/(?P<path>.*)$
The current path, rango/category//add_page/, didn't match any of these. 

Пожалуйста, посмотрите на мои файлы. Следующее - мои views.py.

def add_page(request, category_name_url):
    context = RequestContext(request)
    category_name = decode_url(category_name_url)

    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            page = form.save(commit=False)

            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:

                return render_to_response('rango/add_category.html', {}, context)

            page.views = 0

            page.save()

            return category(request, category_name_url)
        else:
            print (form.errors)
    else:
        form = PageForm()

    return render_to_response( 'rango/add_page.html',
            {'category_name_url': category_name_url,
             'category_name': category_name, 'form': form},
             context)

Это мой urls.py

from django.conf.urls import url
from rango import views

urlpatterns = [
        url(r'^$', views.index, name='index'),
        url(r'about/$', views.about, name='about'),
        url(r'^add_category/$', views.add_category, name='add_category'),
        url(r'^category/(?P<category_name_url>\w+)/$', views.category, name='category'),
        url(r'^category/(?P<category_name_url>\w+)$/add_page/$', views.add_page, name='add_page'),
]        

Это мой add_page.html

<!DOCTYPE html>
<html>
    <head>
        <title>Rango</title>
    </head>

    <body>
        <h1>Add a Page to {{category.name}}</h1><br/>

            <form id="pageI encountered this issue while creating a page in tango_with_django_project.

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/rango/category//add_page/
Using the URLconf defined in tango_with_django_project.urls, Django tried these URL patterns, in this order:
admin/
^rango/ ^$ [name='index']
^rango/ about/$ [name='about']
^rango/ ^add_category/$ [name='add_category']
^rango/ ^category/(?P<category_name_url>\w+)/$ [name='category']
^rango/ ^category/(?P<category_name_url>\w+)$/add_page/$ [name='add_page']
^media\/(?P<path>.*)$
The current path, rango/category//add_page/, didn't match any of these. 

Пожалуйста, посмотрите на мои файлы. Следующее - мои views.py.

def add_page(request, category_name_url):
    context = RequestContext(request)
    category_name = decode_url(category_name_url)

    if request.method == 'POST':
        form = PageForm(request.POST)

        if form.is_valid():
            page = form.save(commit=False)

            try:
                cat = Category.objects.get(name=category_name)
                page.category = cat
            except Category.DoesNotExist:

                return render_to_response('rango/add_category.html', {}, context)

            page.views = 0

            page.save()

            return category(request, category_name_url)
        else:
            print (form.errors)
    else:
        form = PageForm()

    return render_to_response( 'rango/add_page.html',
            {'category_name_url': category_name_url,
             'category_name': category_name, 'form': form},
             context)

Это мой urls.py

from django.conf.urls import url
from rango import views

urlpatterns = [
        url(r'^$', views.index, name='index'),
        url(r'about/$', views.about, name='about'),
        url(r'^add_category/$', views.add_category, name='add_category'),
        url(r'^category/(?P<category_name_url>\w+)/$', views.category, name='category'),
        url(r'^category/(?P<category_name_url>\w+)$/add_page/$', views.add_page, name='add_page'),
]        

Это мой add_page.html

<!DOCTYPE html>_form" method="post" action="/rango/add_page/">

            {% csrf_token %}
            {% for hidden in form.hidden_fields %}
                {{ hidden }}
            {% endfor %}

            {% for field in form.visible_fields %}
                {{ field.errors }}
                {{ field.help_text }}
                {{ field }}
            {% endfor %}

            <input type="submit" name="submit" value="Create Page" />
        </form>
    </body>
</html>

Это мой category.html

<!DOCTYPE html>
<html>
    <head>
        <title>Rango</title>
    </head>

    <body>
        <h1>{{ category_name }}</h1>
        {% if category %}
            {% if pages %}
            <ul>
                {% for page in pages %}
                <li><a href="{{ page.url }}">{{ page.title }}</a></li>
                {% endfor %}
            </ul>
            {% else %}
                <strong>No pages currently in category.</strong><br />
                <strong>Would you like to <a href="/rango/category/{{ category_name_url }}/add_page/">Create one?</a></strong><br />
            {% endif %}

        {% else %}
            The specified category {{ category_name }} does not exist!
        {% endif %}
    </body>
</html>

Пожалуйста, помогите мне решить эту проблему. Эта проблема останавливает меня от изучения фреймворка Django. Я также попробовал то же самое с category_name_slug, но это тоже не сработало.

1
Tech Blogs 13 Мар 2018 в 16:04

2 ответа

Лучший ответ

Изменить строку

<a href="/rango/category/{{ category_name_url }}/add_page/">

К

<a href="/rango/category/{{ category_name }}/add_page/">

Вы отправляете category_name из просмотров, а не category_name_url

Или (лучший способ)

<a href="{% url 'category' category.name %}">
1
anjaneyulubatta505 14 Мар 2018 в 07:14
I'm still getting the following error after changing <a href="/rango/category/{{ category_name_url }}/add_page/"> to <a href="/rango/category/{{ category_name }}/add_page/"> in category.html

This is my error report:

Page not found (404) Request Method:    GET Request URL: 127.0.0.1:8000/rango/category/Movies/add_page Using the URLconf defined in tango_with_django_project.urls, Django tried these URL patterns, in this order: admin/ ^rango/ ^$ [name='index'] ^rango/ about/$ [name='about'] ^rango/ ^add_category/$ [name='add_category'] ^rango/ ^category/(?P<category_name_url>\w+)/$ [name='category'] ^rango/ ^category/(?P<category_name_url>\w+)$/add_page/$ [name='add_page'] ^media\/(?P<path>.*)$ The current path, rango/category/Movies/add_page/, didn't match any of these.
-1
Tech Blogs 14 Мар 2018 в 06:02