簡體   English   中英

Django 表單未顯示在管理頁面上

[英]Django form not showing up on admin page

我在 Django 中有一個聯系表單/模型,它沒有顯示在管理管理頁面上。 我不知道為什么它沒有出現,我嘗試通過運行遷移進行調試,並重命名一些模型變量。

模型.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

表格.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'}))

管理文件

from django.contrib import admin
from .models import Contact
admin.register(Contact)

視圖.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})

這可能是我導入模型的方式嗎? 我不知道是什么導致了這個問題。

在您的admin.py 中為您的 Contact 模型創建一個管理類,然后使用該類在 admin 中注冊此模型。

class ContactAdmin(admin.ModelAdmin):
    list_display = ('id', 'their_name', 'email', 'message') 

admin.site.register(Contact, ContactAdmin)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM