0

当我尝试查看表单时,我只是得到一个空白的 html 页面

我尝试的网址是:http://:8000/catalog/enter_product/

http://screencast.com/t/16JpxtvMTd

尽管存在模块名称:

正如您在此处看到的,settings.py 位于“SOWL”文件夹中,而 CATALOG 是同一级别的不同文件夹 http://screencast.com/t/bsHiglMl7

settings.py 有以下内容。您可以看到“目录”正在其中。

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 类根本没有被使用。

4

1 回答 1

0

考虑到以下几点

render_to_response(template[, dictionary][, context_instance][, mimetype])

在您的CATALOG/views.py文件中,您传递的是context_instancein位置,因此请在函数dictionary中进行以下更改;enter_product

variables = dict(
    form=form
)
于 2012-09-21T15:17:29.817 回答