Django错误 - 没有返回HttpResponse对象

时间:2012-09-13 14:28:24

标签: django httpresponse

在我的主应用程序文件夹中,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对象。

为什么?谢谢你的帮助。

  • 多伦多

1 个答案:

答案 0 :(得分:0)

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