我的数据库未显示在管理面板上

时间:2019-09-16 13:15:54

标签: django python-3.x django-models

models.py

从django.db导入模型

抱怨(models.Model)

name = models.CharField('Name', max_length=120)
Email = models.CharField(max_length=120)
message = models.CharField(max_length = 600)


def __str__(self):
    return self.name

views.py

def contact(request):
if request.method == 'GET':
    form = ComplainForm()
else:
    form = ComplainForm(request.POST)
    if form.is_valid():
        name = form.cleaned_data['name']
        email = form.cleaned_data['Email']
        message = form.cleaned_data['message']
        try:
            send_mail(name, message, email, ['ankushbanik123@gmail.com']) # change this to your email
        except BadHeaderError:
            return HttpResponse('Invalid header found.')
        return redirect('/success')
return render(request, "tem/home.html", {'form': form})

“”“您需要成功提交表单的视图。一旦用户提交了表单,这将呈现感谢消息。然后在urls.py上,导入successView然后添加path('success' ,successView,name ='success'),“”“ def successView(request):     返回render(request,“ tem / success.html”)

form.py

class ComplainForm(forms.ModelForm):

class Meta:
    model = complain
    fields = ['name', 'Email', 'message',]

    def clean_email(self):
        email = self.cleaned_data.get('email')

        return email

admin.py

from django.contrib import admin

在这里注册您的模型。

from .models import complain

admin.site.register(投诉)

class complainAdmin(admin.ModelAdmin):
    list_display = ('name', 'Email', 'message')

admin.site.register(complain, complainAdmin)

1 个答案:

答案 0 :(得分:0)

您的代码在admin.py中看起来正确。您是否已重新加载服务器以刷新Python代码?我亲自为该命令绑定了别名。

alias refresh='sudo systemctl restart gunicorn;sudo systemctl reload nginx'

如果您使用的程序与我使用的程序不同,这对您来说看起来会有所不同。

P.S。 forms.pyviews.py不相关,因为您没有导入它们。