从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
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”)
class ComplainForm(forms.ModelForm):
class Meta:
model = complain
fields = ['name', 'Email', 'message',]
def clean_email(self):
email = self.cleaned_data.get('email')
return email
from django.contrib import admin
from .models import complain
class complainAdmin(admin.ModelAdmin):
list_display = ('name', 'Email', 'message')
admin.site.register(complain, complainAdmin)
答案 0 :(得分:0)
您的代码在admin.py
中看起来正确。您是否已重新加载服务器以刷新Python代码?我亲自为该命令绑定了别名。
alias refresh='sudo systemctl restart gunicorn;sudo systemctl reload nginx'
如果您使用的程序与我使用的程序不同,这对您来说看起来会有所不同。
P.S。 forms.py
和views.py
不相关,因为您没有导入它们。