我是新来的django世界.. 我试图找到关于这个错误的答案,但我没有找到解决我的问题的答案。
urls.py
from django.conf.urls import include, url, patterns
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^menu_direcao/', "academico.views.menu_direcao", name='menu_direçao'),
url(r'^menu_direcao/add_prof/', "academico.views.add_prof", name='add_prof'),
]
if settings.DEBUG:
urlpatterns += patterns('', (r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),)
models.py
from django.db import models
class AddProfessor(models.Model):
nome = models.CharField(max_length=20)
sobrenome = models.CharField(max_length=100)
nascimento = models.DateField()
matricula = models.IntegerField(max_length=20)
senha = models.CharField(max_length=20)
forms.py
from django import forms
from academico.models import AddProfessor
class FormAddProfessor(forms.ModelForm):
class Meta:
model = AddProfessor
fields = 'nome', 'sobrenome', 'nascimento', 'matricula'
views.py
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from academico.models import AddProfessor
from academico.forms import FormAddProfessor
def menu(request):
return render_to_response("menu_temp.html", context_instance = RequestContext(request))
def menu_direcao(request):
return render_to_response("menu_direcao.html", context_instance = RequestContext(request))
def add_prof(request):
if request.method == "POST":
form = FormAddProfessor(request.POST, request.FILES)
if form.is_valid():
form.save()
return render_to_response("salvo.html", {})
else:
form = FormAddProfessor()
return render_to_response("add_professor.html", {'form', form}, context_instance = RequestContext(request))
模板/ add_prof.html
{% extends 'base.html' %}
{% block corpo %}
<form action="" method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Adicionar</button>
</form>
{% endblock %}
错误:
ValueError at /menu_direcao/add_prof/
dictionary update sequence element #0 has length 4; 2 is required
Request Method: GET
Request URL: http://127.0.0.1:8000/menu_direcao/add_prof/
Django Version: 1.8.5
Exception Type: ValueError
Exception Value:
dictionary update sequence element #0 has length 4; 2 is required
Exception Location: C:\Python34\lib\site-packages\django\template\context.py in __init__, line 20
Python Executable: C:\Python34\python.exe
Python Version: 3.4.2
Python Path:
['c:\\Python34\\Scripts\\escola',
'C:\\WINDOWS\\SYSTEM32\\python34.zip',
'C:\\Python34\\DLLs',
'C:\\Python34\\lib',
'C:\\Python34',
'C:\\Python34\\lib\\site-packages']
Server time: Dom, 18 Out 2015 12:50:21 -0200
TRACEBACK:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/menu_direcao/add_prof/
Django Version: 1.8.5
Python Version: 3.4.2
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'academico')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "c:\Python34\Scripts\escola\academico\views.py" in add_prof
49. context_instance = RequestContext(request))
File "C:\Python34\lib\site-packages\django\shortcuts.py" in render_to_response
45. using=using)
File "C:\Python34\lib\site-packages\django\template\loader.py" in render_to_string
115. template_name, context, context_instance, dirs, dictionary)
File "C:\Python34\lib\site-packages\django\template\engine.py" in render_to_string
220. with context_instance.push(context):
File "C:\Python34\lib\site-packages\django\template\context.py" in push
55. return ContextDict(self, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\template\context.py" in __init__
20. super(ContextDict, self).__init__(*args, **kwargs)
Exception Type: ValueError at /menu_direcao/add_prof/
Exception Value: dictionary update sequence element #0 has length 4; 2 is required
所以,如果你能帮助我,我会非常有帮助。
由于
答案 0 :(得分:0)
试试这个观点:
from django.shortcuts import render
def add_prof(request):
if request.method == "POST":
form = FormAddProfessor(request.POST, request.FILES)
if form.is_valid():
form.save()
return render(request, "salvo.html")
else:
form = FormAddProfessor()
return render(request, "add_professor.html", {'form': form})