in my main application folder, the URLs.py has the following code.
在我的主应用程序文件夹中,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),
)
I have a seperate app (Folder) called "CATALOG" and it's URLs.py has the following code:
我有一个名为“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),
)
Under catalog folder, I have forms.py
在目录文件夹下,我有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',)}
and views.py
和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
)
and under "\templates\catalog" folder, I have "enter_product.html", which has the following code:
在“\ 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 %}
but when I go to localhost:8000/catalog/enter_product/ it says:
但是当我去localhost:8000 / catalog / enter_product /它说:
The view CATALOG.views.enter_product didn't return an HttpResponse object.
视图CATALOG.views.enter_product未返回HttpResponse对象。
why is that? Thanks for your help.
这是为什么?谢谢你的帮助。
- Toronto
- 多伦多
1 个解决方案
#1
0
Your return render_to_response...
is indented inside of if request.method == 'POST'
. So if it is not a post, the view function returns nothing. Try unindenting your return statement by one level.
如果request.method =='POST',则返回render_to_response ...缩进。因此,如果它不是帖子,则视图函数不返回任何内容。尝试将您的退货声明取消一级。
#1
0
Your return render_to_response...
is indented inside of if request.method == 'POST'
. So if it is not a post, the view function returns nothing. Try unindenting your return statement by one level.
如果request.method =='POST',则返回render_to_response ...缩进。因此,如果它不是帖子,则视图函数不返回任何内容。尝试将您的退货声明取消一级。