我正在處理某個項目,並且遇到了一些問題。 所以我的表單在我的模板中完全不顯示。 但我之前創建了另一個表單,它的工作原理應該如此!Django窗體不顯示
所以我的代碼:
models.py
class Project(models.Model):
class Meta:
db_table = "project"
COLORS = (
('R', 'Red'),
('B', 'Blue'),
('G', 'Green'),
('Y', 'Yellow')
)
project_title = models.CharField(max_length=200)
project_color = models.CharField(max_length=1, choices=COLORS)
def __str__(self):
return self.project_title
forms.py
class ProjectForm(ModelForm):
class Meta:
model = Project
fields = ['project_title', 'project_color']
views.py
def addproject(request):
if request.POST:
form_p = ProjectForm(request.POST)
if form_p.is_valid():
form_p.save(commit=False)
return HttpResponseRedirect('/')
else:
form_p = ProjectForm()
context = {
'projects': Project.objects.all(),
"form": form_p,
'username': auth.get_user(request).username,
}
context.update(csrf(request))
return render(request, 'index.html', context)
urls.py
urlpatterns = [
url(r'^addproject/$', views.addproject, name='addproject'),]
的index.html
<form action="/addproject/" method="post">
{% csrf_token %}
{{ form_p.as_table }}
<button type="submit" class="btn btn-primary">Add Project</button>
</form>
您已經鍵入了您的上下文變量。 – fledgling