0
votes

models.py

from django.db import models

class complain(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, ['[email protected]']) # change this to your email
        except BadHeaderError:
            return HttpResponse('Invalid header found.')
        return redirect('/success')
return render(request, "tem/home.html", {'form': form})

"""You need a success view for when the form has been submitted. This will render the thank you message once the user has submitted the form. Then on the urls.py, import successView then add path('success', successView, name='success'),""" def successView(request): return 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

Register your models here.

from .models import complain

admin.site.register(complain)

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

admin.site.register(complain, complainAdmin)
1

1 Answers

0
votes

Your code looks correct in admin.py. Have you reloading your server to refresh your Python code? I personally bound an alias to the command to do that.

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

This would look different for you if you're using different programs than me.

P.S. forms.py and views.py are not relevant since your not importing them.