我在django中比较新。我有django的问题..
我想验证我的表格。
这是我的看法......
def submitreq(request):
if request.method == "POST":
sub_req = SubreqForm(request.POST)
if sub_req.is_valid():
success = True
request = sub_req.cleaned_data['request']
category = sub_req.cleaned_data['category']
sub_category = sub_req.cleanded_data['sub_category']
else:
sub_req = subreqForm()
ctx = {'sub_req': sub_req}
return render_to_response("rest/submit_req.html", ctx,context_instance=RequestContext(request))
这是我的模板
<form action = "." class="bs-example form-horizontal" method = "post">
{{sub_req.as_p}}
<div class="form-group">
<label class="col-lg-3 control-label" for="inputText">Request</label>
<div class="col-lg-8">
<input id="inputText" class="form-control" type="Text" placeholder="Request Name" ng-model="request.name"></input>
</div>
<br />
<br />
</div>
<div class="form-group">
<label for="id_category" class="col-lg-3 control-label">Category</label>
<div class="col-lg-8">
<select class="form-control" id="id_category" ng-model="selectedCategory" ng-options="cat.pk as cat.name for cat in category">
<option value="">Select Category</option>
</select>
</div>
</div>
<div class="form-group">
<label for="id_subcategory" class="col-lg-3 control-label">Sub-Category</label>
<div class="col-lg-8">
<select class="form-control" id="id_subcategory" ng-model="selectedSubCategory" ng-options="subcat.id as subcat.name for subcat in subcategory">
<option value="">Select SubCategory</option>
</select>
</div>
</div>
<div>
<center>
<button type="submit" class="btn btn-primary" ng-click="addRequest()">Add Request</button>
</center>
</div>
</form>
这是我的forms.py
from django import forms
from django.core.exceptions import ValidationError
from request.models import Request
class SubreqForm(forms.Form):
request = forms.CharField(max_length=100)
category = forms.CharField()
sub_category = forms.CharField()
这是我的模特
from django.db import models
from base.models import BaseModel
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.contrib.contenttypes import generic
from user_profile.models import Profile
import datetime
from photo.models import *
class Request(BaseModel):
description = models.TextField(blank=True,null=True)
category = models.ForeignKey(Category)
sub_category = models.ForeignKey(SubCategory)
from django.db.models.signals import post_save
from .handlers import *
post_save.connect(
request_handler,
sender=Request,
dispatch_uid='request.handlers.request_handler')
现在为什么会出现这个错误?如果您有任何答案,请帮助我。
答案 0 :(得分:2)
很难说因为你的问题中的缩进需要修复,但我猜这是问题所在:
if request.method == "POST":
sub_req = SubreqForm(request.POST)
if sub_req.is_valid():
如果是request.method != "POST"
,您不会分配到sub_req
,但仍会引用它(在if sub_req.is_valid()
中...)
答案 1 :(得分:0)
很确定它应该是这样的:
def submitreq(request):
if request.method == "POST":
sub_req = SubreqForm(request.POST)
if sub_req.is_valid():
# Submitted form is valid, so do what it says.
success = True
# The following line is blatantly wrong.
# Don't replace the request object with something submitted by form.
request = sub_req.cleaned_data['request']
category = sub_req.cleaned_data['category']
sub_category = sub_req.cleanded_data['sub_category']
else:
# Not a POST (probably a GET) so make a blank form.
sub_req = subreqForm()
# Set up the context and render the response.
ctx = {'sub_req': sub_req}
return render_to_response("rest/submit_req.html",
ctx,context_instance=RequestContext(request))
我认为OP在他们的来源中弄乱了缩进,而不仅仅是问题。