Django'RequestContext'未定义 - forms.ModelForm

 施华洛卉子 发布于 2022-12-07 13:41

我在尝试加载表单时遇到了请求上下文错误.

    在我的models.py上创建了ModelForm

    在我的视图上创建了def添加

    链接到视图的URL

views.py

def add_company(request):
# Get the context from the request.
context = RequestContext(request)

# A HTTP POST?
if request.method == 'POST':
    form = CompanyForm(request.POST)

    # Have we been provided with a valid form?
    if form.is_valid():
        # Save the new category to the database.
        form.save(commit=True)

        # Now call the index() view.
        # The user will be shown the homepage.
        return index(request)
    else:
        # The supplied form contained errors - just print them to the terminal.
        print form.errors
else:
    # If the request was not a POST, display the form to enter details.
    form = CompanyForm()

# Bad form (or form details), no form supplied...
# Render the form with error messages (if any).
return render_to_response('add_company.html', {'form': form}, context)

但它被困在视图的第一行.我和rango教程一样.它有效.但是我的工作不起作用.有人提示吗?

谢谢

请求标题:

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language de,en-US;q=0.7,en;q=0.3
Cache-Control   max-age=0
Connection  keep-alive
Cookie  csrftoken=I9120vmRATOck4a0SSqlfJPLl62PMUOR; sessionid=isx0p4ezb2y9m129v6243ui3ucuyvrak
Host    localhost:8000
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:33.0) Gecko/20100101 Firefox/33.0

响应:

Content-Type    text/html
Date    Sun, 07 Dec 2014 22:01:03 GMT
Server  WSGIServer/0.1 Python/2.7.6
X-Frame-Options SAMEORIGIN

Request Method:     GET
Request URL:    http://127.0.0.1:8000/comp/new  
Django Version:     1.7.1
Exception Type:     NameError
Exception Value:    

name 'models' is not defined

Exception Location:     /home/mandaro/django/comp/company/forms.py in CompanyForm, line 5
Python Executable:  /usr/bin/python
Python Version:     2.7.6

得到它了:

Problem wasn t on form - it was template import problem. Imported render_to_response instead of render solved it. Now it can goes on. ciao and tx

小智.. 7

你忘记导入RequestContext了吗?

from django.template import RequestContext


doniyor.. 6

你实际上不需要担心传递RequestContext,因为如果你使用render(),它会为你处理它.

所以你会这样做:

return render(request, 'add_company.html', {'form': form})

代替

return render_to_response('add_company.html', {'form': form}, context)

而已.当然,你也需要导入它.

from django.shortcuts import render

希望,这解决了你的问题

2 个回答
  • 你实际上不需要担心传递RequestContext,因为如果你使用render(),它会为你处理它.

    所以你会这样做:

    return render(request, 'add_company.html', {'form': form})
    

    代替

    return render_to_response('add_company.html', {'form': form}, context)
    

    而已.当然,你也需要导入它.

    from django.shortcuts import render
    

    希望,这解决了你的问题

    2022-12-11 03:08 回答
  • 你忘记导入RequestContext了吗?

    from django.template import RequestContext
    

    2022-12-11 03:12 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有