-2

I fill the Django form in contact.html file. But form data is not saved in database or another place. There is no error or warning while saving the form data.

Form screenshot:

Form screenshot.

views.py:

from .forms import CnForm def contact(request): template = loader.get_template('contact.html') form = CnForm(request.POST or None) if form.is_valid(): form.save() context = {'form': form } return HttpResponse(template.render(context, request)) 

models.py:

from django.db import models class FModel(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) def __str__(self): return self.first_name 

forms.py:

from django import forms from .models import FModel class CnForm(forms.ModelForm): class Meta: model = FModel fields = "__all__" 

contact.html:

 <div class="contact-container"> <form action = "" method = "post"> {% csrf_token %} {{ form }} <input type="submit" value="Submit"> </form> </div> 

urls.py:

urlpatterns = [ . . . path('contact', views.contact, name='contact'), ] 
8
  • You're not doing anything special after saving the form. Are you sure it's not being saved, or are you just saying that because it just reloads the form? Commented Nov 9 at 7:18
  • Share the urls.py. Commented Nov 9 at 7:21
  • Yes. It just reloads form after clicking Submit button. Commented Nov 9 at 7:21
  • Yes, it does, because that's what your code tells it to do. Have you checked the admin or database directly, whether the record was saved there? Commented Nov 9 at 7:22
  • There is no record about the form in the database. I checked the admin page. Commented Nov 9 at 7:25

1 Answer 1

0

Form information is visible by using admin page after adding the model into admin.py file.

The form data was saved but it was not visible when admin page is used.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.