创建页面时Django错误

时间:2018-03-13 13:04:22

标签: python django

我在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,但这也不行。

2 个答案:

答案 0 :(得分:1)

更改行

<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 :(得分:-1)

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.