Django表单提交导致404错误

时间:2019-01-27 23:40:53

标签: python django post django-forms http-status-code-404

用户提交表单时,我收到以下错误:

Resources:
FrontEndBucket:
Type: 'AWS::S3::Bucket'
Properties:
  BucketName: !If 
    - IsProduction
    - 'Fn::ImportValue': !Sub '${LandscapeStack}-DNSDomain'
    - 'Fn::Sub':
        - '${EnvironmentSubdomain}.${LandscapeDomain}'
        - LandscapeDomain:
            'Fn::ImportValue': !Sub '${LandscapeStack}-DNSDomain'
  AccessControl: Private
  Tags:
    - Key: landscape
      Value: !Ref Landscape
    - Key: environment
      Value: !Ref EnvironmentName
  WebsiteConfiguration:
    IndexDocument: index.html
Metadata:
  'AWS::CloudFormation::Designer':
    id: 53eb6c3a-767f-46fb-a719-2846f1fd6bdd
FrontEndBucketPolicy:
Type: 'AWS::S3::BucketPolicy'
Properties:
  Bucket: !Ref FrontEndBucket
  PolicyDocument:
    Statement:
      - Effect: Allow
        Action:
          - 's3:PutObject'
          - 's3:GetObject'
          - 's3:DeleteObject'
        Resource: !Sub 'arn:aws:s3:::${FrontEndBucket}/*'
        Principal:
          AWS:
            'Fn::ImportValue': !Sub '${CIResourcesStack}-BitbucketFrontEndPipelineUser'
      - Effect: Allow
        Action:
          - 's3:ListBucket'
        Resource: !Sub 'arn:aws:s3:::${FrontEndBucket}'
        Principal:
          AWS:
            'Fn::ImportValue': !Sub '${CIResourcesStack}-BitbucketFrontEndPipelineUser'
      - Effect: Allow
        Action:
          - 's3:GetObject'
        Resource: !Sub 'arn:aws:s3:::${FrontEndBucket}/*'
        Principal:
          CanonicalUser: !GetAtt FrontEndCDNAccessIdentity.S3CanonicalUserId
Metadata:
  'AWS::CloudFormation::Designer':
    id: f8d2a536-790c-4cbe-adcb-b50754c0922e

似乎我要追加rango / add_category两次,而不是重新引用索引页面。但是我不确定我在忽略什么。

以下是相关模板:

Page not found (404)
Request Method: csrfmiddlewaretoken=uaL0Ogej2bd0oSaNLXYwu1CxSPWz6mcs0PuXiwM2mpe01VecK5IVBK40xvqcFCJF&views=0&likes=0&slug=&name=do+do+ahadalfjkdas%3Bldfjksal%3B12321&submit=Create+CategoryPOST
Request URL:    http://127.0.0.1:8000/rango/add_category/rango/add_category/
Using the URLconf defined in tango_with_django_project.urls, Django tried these URL patterns, in this order:

^$ [name='index']
^admin/
^rango/ ^$ [name='index']
^rango/ ^about/ [name='about']
^rango/ ^category/(?P<category_name_slug>[\w\-]+)/$ [name='show_category']
^rango/ ^page/(?P<page_name_slug>[\w\-]+)/$ [name='show_page']
^rango/ ^add_category/$ [name='add_category']
The current path, rango/add_category/rango/add_category/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

以及相关的表格文件:

<!-- created in c7 for form viewing-->

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

    <body>
        <h1>Add a Category</h1>
        <div>
            <form id="category_form" method="post" action="rango/add_category/">
                {% 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 Category" />
            </form>
        </div>
    </body>
</html>

相关的url文件:

#file added c7, forms 
from django import forms
from rango.models import Page, Category

class CategoryForm(forms.ModelForm):
    name = forms.CharField(max_length=128,
                        help_text="Please enter the category name.")
    views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
    likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
    slug = forms.CharField(widget=forms.HiddenInput(), required=False)

    #Inline class to provide additional info on the form
    class Meta:
        #provide an association b/t ModelForm and model
        model = Category
        fields = ('name',)

视图中的相关视图:

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^about/', views.about, name='about'),
    #?P makes group to match the slug 
    url(r'^category/(?P<category_name_slug>[\w\-]+)/$',
    views.show_category, name='show_category'),
    #page slug added at ex at end of 6
    # not sure if needed, given index view ...
    url(r'^page/(?P<page_name_slug>[\w\-]+)/$',
    views.show_page, name='show_page'),
    #show page added in ex at end of 6
    #next added at c7 for forms
    #ordering may matter for processing of requests -- see official docs 
    url(r'^add_category/$', views.add_category, name='add_category')]

谢谢。

1 个答案:

答案 0 :(得分:2)

尝试更改表单的操作网址

<form id="category_form" method="post" action="{% url 'add_category' %}">

有关更多信息,请阅读https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#url