【发布时间】:2017-06-21 16:38:29
【问题描述】:
我在 Django 中有一个联系表单/模型,它没有显示在管理员管理页面上。 我不知道为什么它没有出现,我试图通过运行迁移进行调试,并重命名一些模型变量。
models.py
from django.db import models
class Contact(models.Model):
their_name = models.CharField(max_length=100)
email = models.EmailField(max_length=100)
message = models.CharField(max_length=500)
def __str__(self):
return self.name
forms.py
from django import forms
class ContactForm(forms.Form):
their_name = forms.CharField(label='Name', max_length=100)
email = forms.EmailField(label='Email', max_length=100)
message = forms.CharField(label='Message', max_length=500, widget=forms.Textarea(attrs = {'id': 'Message_form'}))
admin.py
from django.contrib import admin
from .models import Contact
admin.register(Contact)
views.py
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from .models import Contact
from .forms import ContactForm
...
def contact(request):
valid_input = 'no input'
if request.method == 'POST':
valid_input = 'invalid input'
TheForm = ContactForm(request.POST)
if TheForm.is_valid():
valid_input = 'valid input'
name = TheForm['name']
email = TheForm['email']
message = TheForm['message']
Contact.objects.create(name=name, email=email, message=message)
else:
TheForm = ContactForm()
return render(request, 'BlogHome/pages/contact.html', {'TheForm': TheForm, 'valid_input': valid_input})
这可能是我导入模型的方式吗?我不知道是什么导致了这个问题。
【问题讨论】:
-
表单和视图与管理页面有什么关系?
-
与表单关联的模型未显示。
-
在管理员中?那么,您是否已将该应用程序包含在 INSTALLED_APPS 中?同样,这与视图和表单无关,它们在管理中没有使用。
-
是的。此应用程序位于 INSTALLED_APPS 中。我之所以提到表格,是因为过去有模型的表格给我带来了问题。
标签: python django django-forms django-admin django-1.8