Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django中未绑定的LocalError_Python_Django - Fatal编程技术网

Python django中未绑定的LocalError

Python django中未绑定的LocalError,python,django,Python,Django,我是django的新手。我对django有问题 错误为/app/rest/submitreq\u val处的UnboundLocalError/ 分配前引用的局部变量“sub_req” 我想验证我的表单 这是我的观点 def submitreq(request): if request.method == "POST": sub_req = SubreqForm(request.POST) if sub_req.is_valid(): success = True r

我是django的新手。我对django有问题

错误为/app/rest/submitreq\u val处的UnboundLocalError/ 分配前引用的局部变量“sub_req”

我想验证我的表单

这是我的观点

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>
这是我的模型

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

现在为什么会发生这个错误??如果您有任何答案,请帮助我。

很难说,因为您问题中的缩进需要修复,但我想这就是问题所在:

if request.method == "POST":
    sub_req = SubreqForm(request.POST)

if sub_req.is_valid():

如果
request.method!=“POST”
,您没有分配给
sub\u-req
,但您仍然引用它(在
中,如果sub\u-req.is\u有效()
…)

非常确定它应该是这样的:

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搞乱了源代码中的缩进,而不仅仅是问题。

是的,整个
if sub\u req.is\u valid():
块需要缩进一级。如果
request.method!='发布“
.zogo,如果你觉得答案有帮助,请随意接受它;)
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))