需要 Django“局部变量”表单的帮助赋值前引用

发布于 2024-09-26 03:55:07 字数 1669 浏览 5 评论 0原文

我在 django 中遇到问题。我在我的应用程序中创建了一个表单,可以在其中获取客户的详细信息。现在我想创建一个可以让我编辑表单的表单。但是,当我转到 /index/edit_client/1 时遇到一些问题,出现此错误。

local variable 'form' referenced before assignment

我不知道出现此错误的原因是什么,但从我所看到的来看,它没有任何帮助,除非当然有另一种方法如何创建编辑表单来编辑客户表单。以下是一些也很有帮助的输出。

# urls.py
    urlpatterns = patterns('',
    (r'^index/$', login_required(direct_to_template), { 'template': 'index.html' }),
    (r'^index/clients/$', client_info),
    (r'^index/clients_details/(?P<id>\d+)/$', clients_details),
    (r'^index/edit_client/(?P<id>\d+)/$', edit_client),
)

# views.py
@login_required 
def edit_client(request, id=1):
    clients_list = Client.objects.filter(pk=id)  
    if request.method == 'POST':
        form = ClientForm(request.POST or None)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/index/clients/')
        else: form = ClientForm()
    return render_to_response('edit_client.html', {'form': form},  context_instance=RequestContext(request))

#edit_client.html
{% extends "base.html" %}

{% block content %}
<font face="verdana,news gothic,arial,heltevica,serif">
    <h3>Edit Client</h3>
</font>
<form method= "POST" action="">
    <font face="verdana,news gothic,arial,heltevica,serif">
    <div id="form">
        <table>
            {{form.as_table}}
        </table>
        <div align="center" STYLE=" margin-right:190px">
            <input type="submit" value="Submit" STYLE="background-color:#E8E8E8; color:#181818 "/>
        </div>
    </div>
</form>
{% endblock %}

I am having problem in django. I have created a form in my app where I can take details of a client. Now I want to create a form which can allow me to edit a form. However I am having some problems when I go to /index/edit_client/1, I get this error.

local variable 'form' referenced before assignment

I do not know what the reason why I have got this error, but from what I have looked at, it does not help matters unless of course there is another way how to create an edit form to edit the clients form. Here are some output that can be helpful too.

# urls.py
    urlpatterns = patterns('',
    (r'^index/
, login_required(direct_to_template), { 'template': 'index.html' }),
    (r'^index/clients/
, client_info),
    (r'^index/clients_details/(?P<id>\d+)/
, clients_details),
    (r'^index/edit_client/(?P<id>\d+)/
, edit_client),
)

# views.py
@login_required 
def edit_client(request, id=1):
    clients_list = Client.objects.filter(pk=id)  
    if request.method == 'POST':
        form = ClientForm(request.POST or None)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/index/clients/')
        else: form = ClientForm()
    return render_to_response('edit_client.html', {'form': form},  context_instance=RequestContext(request))

#edit_client.html
{% extends "base.html" %}

{% block content %}
<font face="verdana,news gothic,arial,heltevica,serif">
    <h3>Edit Client</h3>
</font>
<form method= "POST" action="">
    <font face="verdana,news gothic,arial,heltevica,serif">
    <div id="form">
        <table>
            {{form.as_table}}
        </table>
        <div align="center" STYLE=" margin-right:190px">
            <input type="submit" value="Submit" STYLE="background-color:#E8E8E8; color:#181818 "/>
        </div>
    </div>
</form>
{% endblock %}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-10-03 03:55:07

这将始终运行:

return render_to_response('edit_client.html', {'form': form}

但是如果 request.method 不是 POST,则不会向 form 分配任何内容。

固定代码:

@login_required 
def edit_client(request, id=1):
    clients_list = Client.objects.filter(pk=id)  
    form = ClientForm()
    if request.method == 'POST':
       form = ClientForm(request.POST or None)
       if form.is_valid():
           form.save()
           return HttpResponseRedirect('/index/clients/')
    return render_to_response('edit_client.html', {'form': form},  context_instance=RequestContext(request))

This will always run:

return render_to_response('edit_client.html', {'form': form}

But if request.method is not POST, nothing is assigned to form.

Fixed code:

@login_required 
def edit_client(request, id=1):
    clients_list = Client.objects.filter(pk=id)  
    form = ClientForm()
    if request.method == 'POST':
       form = ClientForm(request.POST or None)
       if form.is_valid():
           form.save()
           return HttpResponseRedirect('/index/clients/')
    return render_to_response('edit_client.html', {'form': form},  context_instance=RequestContext(request))
ま昔日黯然 2024-10-03 03:55:07

在您的 edit_client 方法中,您在响应中传递 form,但是,如果该方法不是 POST,您将不会初始化 form代码>.

In your edit_client method, you pass form in the response, however, if the method wasn't a POST, you won't have initialized a form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文