简体   繁体   中英

The current path, product/, didn't match any of these

I am having trouble with connecting to url code.

Everything is showing fine but after I tried to create, this error message pops up.

Also, I would like to know if path function works on the django 1.x.

'''error message'''

Page not found (404)
Request Method: POST
Request URL:    http://127.0.0.1:8000/product/
Using the URLconf defined in seany.urls, Django tried these URL patterns, in this order:

^admin/
^$
^register/$
^login/$
^product/create/
The current path, product/, didn't match any of these.

'''url.py'''

from django.conf.urls import url
from django.contrib import admin
from seany_user.views import index, registerview, loginview
from seany_product.views import productlist, productcreate
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', index),
    url(r'^register/$', registerview.as_view()),
    url(r'^login/$', loginview.as_view()),
    url(r'^product/create/', productcreate.as_view())
]

'''form.py'''

from django import forms
from seany_product.models import seanyproduct

class registerform(forms.Form):
    name = forms.CharField(
        error_messages={
            'required': 'enter your goddamn product'
        },
        max_length=64, label='product'
    )
    price = forms.IntegerField(
        error_messages={
            'required': 'enter your goddamn price'
        }, label='price'
    )
    description = forms.CharField(
        error_messages={
            'required': 'enter your goddamn description'
        }, label='description'
    )
    stock = forms.IntegerField(
        error_messages={
            'required': 'enter your goddamn stock'
        }, label='stock'
    )

    def clean(self):
        cleaned_data = super().clean()
        name = cleaned_data.get('name')
        price = cleaned_data.get('price')
        description = cleaned_data.get('description')
        stock = cleaned_data.get('stock')

        if name and price and description and stock:
            seany_product = product(
                name=name,
                price=price,
                description=description,
                stock=stock
            ) 
            seany_product.save()

'''views.py'''

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.views.generic import ListView
from django.views.generic.edit import FormView
from django.shortcuts import render
from seany_product.models import seanyproduct
from seany_product.forms import registerform
# Create your views here.

class productlist(ListView):
    model = seanyproduct
    template_name = 'product.html'
    context_object_name = 'product_list'

class productcreate(FormView):
    template_name = 'register_product.html'
    form_class = registerform
    success_url = '/product/'

In the last piece of code, you are trying to direct to /product/ url,

class productcreate(FormView):
    template_name = 'register_product.html'
    form_class = registerform
    success_url = '/product/' # <--- this

but in the urls.py urlpatterns , you have not defined any url /product . Note that /product/create is not same as /product/ , which is why Django cannot find any thing in response to /product url, and returns 404 error.

To fix this, add a url in urlpatterns like - url(r'^product/$', productlist.as_view()) , or any other view as needed; you will also have to create this view.

Basically, Django does not know which page to show for /product url. You have to define that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM