PaladinNoGames 1702
PaladinNoGames 1702

Reputation: 1

Contactform model displays no content in the Django admin panel. Django admin panel module values are empty

so I am trying to make an simple contact form for my website and Technically I am finished with this.

But whenever i try to test and submit something through this form, the input values are not showing in the admin panel.

I get no errors but the i don't see any code

For example: (https://i.sstatic.net/plMjM.png) This should add 3 values into the admin panel

But instead (https://i.sstatic.net/DQR7U.png) does it add nothing into the admin panel.

The code should work like this:

  1. The user should write a message in the HTML.
<section class="contact-me">
              <h2>Kontaktiere mich</h2>
              <form method="POST">
                {% csrf_token %}
                <h4>Vorname:</h4>
                <input type="text" placeholder="Vorname" name="vorname">
                <h4>Email:</h4>
                <input type="email" placeholder="[email protected]" name="email">
                <h4>Nachicht/Frage:</h4>
                <textarea name="nachricht" id="" cols="30" rows="10"></textarea> <br>
                <input type="submit" value="submit">
              </form>
            </section>
  1. In the views.py file there is this code witch should take the inputs and convert them into the model.

views.py

def aboutme(request):
    if request.method=="POST":
        contact = ContactForm()
        vorname = request.POST["vorname"]
        email = request.POST["email"]
        nachricht = request.POST["nachricht"]

        contact.name = vorname
        contact.email = email
        contact.nachricht = nachricht

        contact.save()
        return HttpResponse("<h1>Danke für deine Nachricht</h1>")
    return render(request, "aboutme.html")

models.py

class ContactForm(models.Model):
    vorname = models.CharField(max_length=15)
    email = models.EmailField(max_length=20)
    nachricht = models.TextField(max_length=1000)
    def __str__(self):
        return self.vorname

The admin panel shows that there is an entry but it doesn't show the inputs ?

Does anyone have an clue why this is ?

I think it doesn't have anython to do with the HTML or the views.py i think that it has something to do with the model itself but i dont know.

Please help me if you have time.

Upvotes: 0

Views: 70

Answers (1)

amirhossein amiri
amirhossein amiri

Reputation: 151

edit your views.py, you have to send request.POST into your ContactForm():

def aboutme(request):
    if request.method=="POST":
        contact = ContactForm(request.POST)  #
        vorname = request.POST["vorname"]
        email = request.POST["email"]
        nachricht = request.POST["nachricht"]

        contact.name = vorname
        contact.email = email
        contact.nachricht = nachricht

        contact.save()
        return HttpResponse("<h1>Danke für deine Nachricht</h1>")
    return render(request, "aboutme.html")

but if your ContactForm is look like this:

class ContactForm(forms.Form):
    vorname = forms.CharField()
    email = forms.EmailField()
    nachricht = forms.CharField()

your view should be like this, short and simple:

def aboutme(request):
    if request.method=="POST":
        contact = ContactForm(request.POST)
        contact.save()
        return HttpResponse("<h1>Danke für deine Nachricht</h1>")
    return render(request, "aboutme.html")

Upvotes: 1

Related Questions