我无法连接到网址代码。
一切正常,但是在我尝试创建后,此错误消息弹出。
此外,我想知道django 1.x是否可以使用path函数。
'''错误消息'''
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/'
答案 0 :(得分:0)
在最后一段代码中,您尝试直接指向/product/
网址
class productcreate(FormView):
template_name = 'register_product.html'
form_class = registerform
success_url = '/product/' # <--- this
,但是在urls.py urlpatterns
中,您尚未定义任何URL /product
。请注意,/product/create
与/product/
不同,这就是Django无法找到任何内容来响应/product
网址并返回404错误的原因。
要解决此问题,请在urlpatterns
中添加一个网址,例如-
url(r'^product/$', productlist.as_view())
或其他所需的视图;您还必须创建此视图。
基本上,Django不知道显示/product
网址的页面。您必须定义它。