当我尝试查看表单时,我只是得到一个空白的html页面
我尝试的网址是:http://:8000 / catalog / enter_product /
http://screencast.com/t/16JpxtvMTd
虽然存在模块名称:
正如您在此处所见,settings.py位于“SOWL”文件夹中,CATALOG是同一级别的不同文件夹 http://screencast.com/t/bsHiglMl7
settings.py具有以下内容。你可以看到他们正在使用“CATALOG”。
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'CATALOG',
'SOWLAPP',
'registration',
)
在SOWL / URls.py中,您可以看到以下代码。在主URL中调用“目录”的位置。它查找CATALOG.urls文件。
urlpatterns = patterns('',
url (r'^user/(\w+)/$', user_page),
(r'^login/$', 'django.contrib.auth.views.login'),
# (r'^catalog/$', home),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root' : 'C:/SHIYAM/Personal/SuccessOwl/SOWL0.1/SOWL/SOWL/static'}),
# (r'^admin/', include('django.contrib.admin.urls')),
(r'^catalog/', include('CATALOG.urls')),
(r'^accounts/', include('registration.urls')),
(r'^$', main_page),
)
CATALOG.urls
from django.conf.urls.defaults import *
from CATALOG.views import *
urlpatterns = patterns('SOWL.catalog.views',
(r'^$', 'index', { 'template_name':'catalog/index.html'}, 'catalog_home'),
(r'^category/(?P<category_slug>[-\w]+)/$', 'show_category', {'template_name':'catalog/category.html'},'catalog_category'),
(r'^product/(?P<product_slug>[-\w]+)/$', 'show_product', {'template_name':'catalog/product.html'},'catalog_product'),
(r'^enter_product/$',enter_product),
)
CATALOG / models.py有此类
class Product(models.Model):
name = models.CharField(max_length=255, unique=True)
slug = models.SlugField(max_length=255, unique=True, help_text='Unique value for product page URL, created from name.')
brand = models.CharField(max_length=50)
sku = models.CharField(max_length=50)
price = models.DecimalField(max_digits=9,decimal_places=2)
old_price = models.DecimalField(max_digits=9,decimal_places=2, blank=True,default=0.00)
image = models.CharField(max_length=50)
is_active = models.BooleanField(default=True)
is_bestseller = models.BooleanField(default=False)
is_featured = models.BooleanField(default=False)
quantity = models.IntegerField()
description = models.TextField()
meta_keywords = models.CharField(max_length=255, help_text='Comma-delimited set of SEO keywords for meta tag')
meta_description = models.CharField(max_length=255, help_text='Content for description meta tag')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
categories = models.ManyToManyField(Category)
user = models.ForeignKey(User)
class Meta:
db_table = 'products'
ordering = ['-created_at']
def __unicode__(self):
return self.name
@models.permalink
def get_absolute_url(self):
return ('catalog_product', (), { 'product_slug': self.slug })
def sale_price(self):
if self.old_price > self.price:
return self.price
else:
return None
CATALOG / forms.py有这个类:
class Product_Form(forms.Form):
name = forms.CharField(label='name', max_length=30)
slug = forms.SlugField(label='Unique Name for the URL', max_length=30)
brand = forms.CharField(label='Unique Name for the URL', max_length=30)
price = forms.DecimalField(label='Price',max_digits=9,decimal_places=2)
old_price = forms.DecimalField(max_digits=9,decimal_places=2,initial=0.00)
quantity = forms.IntegerField()
description = forms.CharField()
meta_keywords = forms.CharField(max_length=255)
meta_description = forms.CharField(max_length=255)
categories = forms.CharField(max_length=255)
user = forms.IntegerField()
prepopulated_fields = {'slug' : ('name',)}
CATALOG / views.py有:
# Create your views here.
from CATALOG.forms import *
from django.template import RequestContext
from django.shortcuts import render_to_response
def enter_product(request):
if request.method == 'POST':
form = Product_Form(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.clean_data['username'],
password=form.clean_data['password1'],
email=form.clean_data['email']
)
return HttpResponseRedirect('/')
else:
form = Product_Form()
variables = RequestContext(request, {
'form': form
})
return render_to_response('catalog/enter_product.html',variables)
SOWL \ templates \ catalog \ enter_product.html具有以下代码
{% extends "base.html" %}
{% block title %}blah{% endblock %}
{% block head %}blah{% endblock %}
{% block content %}
<form method="post" action=".">
{{ form.as_p }}
<input type="submit" value="Enter" />
</form>
{% endblock %}
SOWL \ templates \ catalog \ base.html具有以下代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Django Bookmarks |
{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="/site_media/style.css"
type="text/css" />
</head>
<body>
<div id="nav">
<a href="/">home</a> |
{% if user.is_authenticated %}
welcome {{ user.username }}
(<a href="/logout">logout</a>)
{% else %}
<a href="/login/">login</a>
{% endif %}
</div>
<h1>{% block head %}{% endblock %}</h1>
{% block content %}{% endblock %}
</body>
</html>
我觉得它与我的观点创建方式有关。我没有看到视图的product_form类没有被使用。
答案 0 :(得分:0)
记住以下内容
render_to_response(template[, dictionary][, context_instance][, mimetype])
您在CATALOG/views.py
位置的context_instance
传递dictionary
文件,因此请在enter_product
函数中进行以下更改;
variables = dict(
form=form
)