我在连接到网址代码时遇到问题。
一切都显示良好,但是在我尝试创建之后,弹出了此错误消息。
另外,我想知道路径函数是否适用于 django 1.x。
"错误消息">
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/'
在最后一段代码中,您尝试定向到/product/
url,
class productcreate(FormView):
template_name = 'register_product.html'
form_class = registerform
success_url = '/product/' # <--- this
但是在 urls.pyurlpatterns
中,您没有定义任何URL/product
。请注意,/product/create
与/product/
不同,这就是为什么 Django 找不到任何响应 url/product
并返回 404 错误的原因。
要解决此问题,请在urlpatterns
中添加一个网址,例如 -url(r'^product/$', productlist.as_view())
,或根据需要的任何其他视图;您还必须创建此视图。
基本上,Django 不知道/product
url 显示哪个页面。你必须定义它。