Django'RequestContext'没有定义 - forms.ModelForm [英] Django 'RequestContext' is not defined - forms.ModelForm

查看:169
本文介绍了Django'RequestContext'没有定义 - forms.ModelForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 在我的models.py上创建了ModelForm

  2. 在我的视图上创建def添加

  3. 链接到视图

views.py

  def add_company(request):
#从请求中获取上下文。
context = RequestContext(request)

#HTTP POST?
如果request.method =='POST':
form = CompanyForm(request.POST)

#我们是否已提供有效的表单?
如果form.is_valid():
#将新类别保存到数据库。
form.save(commit = True)

#现在调用index()视图。
#用户将显示首页。
返回索引(请求)
else:
#提供的表单包含错误 - 只需将其打印到终端。
print form.errors
else:
#如果请求不是POST,则显示窗体以输入详细信息。
form = CompanyForm()

#表单(或表单详细信息),不提供表单
#渲染带有错误消息的表单(如果有)。
return render_to_response('add_company.html',{'form':form},context)

但它被困在视图的第一行。我做了相同的在rango教程。它有效但我的工作不是。任何一个提示?



谢谢



请求标题:

 接受文本/ 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
缓存控制max-age = 0
连接keep-alive
Cookie csrftoken = I9120vmRATOck4a0SSqlfJPLl62PMUOR; sessionid = isx0p4ezb2y9m129v6243ui3ucuyvrak
主机本地主机:8000
用户代理Mozilla / 5.0(X11; Ubuntu; Linux i686; rv:33.0)Gecko / 20100101 Firefox / 33.0

回应:

 内容类型text / html 
日期太阳,2014年12月07日22:01:03 GMT
服务器WSGIServer / 0.1 Python / 2.7.6
X框架选项SAMEORIGIN

请求方法: GET
请求URL:http://127.0.0.1:8000/comp/new
Django版本:1.7.1
异常类型:NameError
异常值:

名称'models'未定义

异常位置:/home/mandaro/django/comp/company/forms.py在CompanyForm第5行
Python可执行文件:/ usr / bin / python
Python版本:2.7.6

GOT it:

 表单上没有问题 - 这是模板导入问题。导入的render_to_response而不是渲染解决它。现在可以继续ciao和tx 


解决方案

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



所以你可以这样做:

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

而不是

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

就可以了。当然,您也需要导入它。

  from django.shortcuts import render 

希望,这可以解决您的问题


Ive got the Request Context Error when trying to load my Form.

  1. Created ModelForm on my models.py
  2. created def add on my view
  3. linked url to the view

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)

But its get stuck on the first line of the view. I made it same as on rango tutorial. There it works. But mine isnt working. Anyone a hint?

thanks

Request Header:

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

Response:

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

GOT it:

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

解决方案

you actually dont need to worry about passing RequestContext, because if you use render(), it handles it for you.

so you would do:

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

instead of

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

thats it. of course, you need to import it as well.

from django.shortcuts import render

Hope, this solves your problem

这篇关于Django'RequestContext'没有定义 - forms.ModelForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆