-1

在我的主应用程序文件夹中,URLs.py 具有以下代码。

urlpatterns = patterns('',
    (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.py 具有以下代码:

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),
)

在目录文件夹下,我有 forms.py

from django import forms
from CATALOG.models import Product

class ProductAdminForm(forms.ModelForm):
    class Meta:
        model = Product

def clean_price(self):
    if self.cleaned_data['price'] <= 0:
        raise forms.ValidationError('Price must be greater than zero.')
    return self.cleaned_data['price']

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',)}

和views.py

from CATALOG.forms import *
def enter_product(request):
    if request.method == 'POST':
        form = RegistrationForm(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 = RegistrationForm()
        variables = RequestContext(request, {
            'form': form
        })

        return render_to_response(
            'catalog/enter_product.html',
            variables
        )

在“\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 %}

但是当我去 localhost:8000/catalog/enter_product/ 它说:

视图 CATALOG.views.enter_product 没有返回 HttpResponse 对象。

这是为什么?谢谢你的帮助。

  • 多伦多
4

1 回答 1

0

return render_to_response...的缩进在if request.method == 'POST'. 因此,如果它不是帖子,则视图函数不会返回任何内容。尝试将您的 return 语句取消缩进一级。

于 2012-09-13T14:30:48.383 回答