Python 局部变量';上下文';在django中分配前引用
当使用GET方法进行请求时,您正在发送上下文而没有声明它Python 局部变量';上下文';在django中分配前引用,python,django,Python,Django,当使用GET方法进行请求时,您正在发送上下文而没有声明它 from django.shortcuts import render from main import (models,forms) from django.http import HttpResponseRedirect # Create your views here. def index(request): return render (request, 'index.txt') def about(reques
from django.shortcuts import render
from main import (models,forms)
from django.http import HttpResponseRedirect
# Create your views here.
def index(request):
return render (request, 'index.txt')
def about(request):
return render (request, 'about.txt')
def contact(request):
contactform = forms.ContactForm()
if request.method == "POST":
contactform = forms.ContactForm(request.POST)
if contactform.is_valid():
contact = contactform.save()
return HttpResponseRedirect('/contact')
context = {
"contactform" : contactform
}
return render(request, 'contact.txt', context)
def post(request):
return render (request, 'post.txt')
如果请求方法是GET,会发生什么?
from django.shortcuts import render
from main import (models,forms)
from django.http import HttpResponseRedirect
# Create your views here.
def index(request):
return render (request, 'index.txt')
def about(request):
return render (request, 'about.txt')
def contact(request):
if request.method == "GET":
context = {
"contactform" : forms.ContactForm()
}
if request.method == "POST":
contactform = forms.ContactForm(request.POST)
if contactform.is_valid():
contact = contactform.save()
return HttpResponseRedirect('/contact')
context = {
"contactform" : contactform
}
return render(request, 'contact.txt', context)
def post(request):
return render (request, 'post.txt')