Django表单提交无响应且数据无法保存问题求助
Hey there! Let's dig into why your forms aren't working and fix them step by step. I spotted several key issues in your code that are causing the submission to fail:
1. Incorrect Form Instance in Your Template View
Your Anasayfa_View is incorrectly inheriting from Kesif_Form (a form class, not a view) and passing the form class instead of an instance to the template. This means Django can't render the form properly for user input.
Fix the Anasayfa_View:
from django.views.generic import TemplateView from forms.formlar import Kesif_Form class Anasayfa_View(TemplateView): template_name = 'index.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # Pass an INSTANCE of the form, not the class itself context['kform'] = Kesif_Form() # For your future second form, instantiate it here too: # context['second_form'] = YourSecondForm() return context
2. Missing CSRF Token in the Template
Django requires a CSRF token for all POST forms to prevent cross-site request forgery. Without it, your form submission will be silently rejected by Django's security middleware. Add this critical line inside your form tag:
... {{ kform.errors }} <form action="{% url 'kesif' %}" method="POST" class="php-email-form php-email-form animate__animated animate__fadeInRight"> {% csrf_token %} <!-- Add this line! --> {{ kform.errors }} {{ kform }} <input type="submit" value="gir"> </form> ...
3. Redundant Manual Data Assignment in the kesif_kayit View
You're using a ModelForm but manually pulling every field from request.POST—this defeats the purpose of using a ModelForm and introduces avoidable errors (like passing a string to a DateTimeField which expects a datetime object). Use the form's built-in save() method instead:
from django.shortcuts import redirect, render from django.forms import ModelForm from apps.giris.models import Iletisim class Kesif_Form(ModelForm): class Meta: model = Iletisim fields = '__all__' def kesif_kayit(request): if request.method == 'POST': form = Kesif_Form(request.POST) if form.is_valid(): # The ModelForm handles all field assignments automatically form.save() # Redirect to your homepage using its correct URL name 'grsndx' return redirect('grsndx') else: form = Kesif_Form() context = {'form': form} return render(request, 'index.html', context)
4. Prepping for Your Second Form
When adding a second form later, use unique prefix attributes to avoid field name conflicts between the two forms. Here's how to set this up:
Update Anasayfa_View for two forms:
from forms.formlar import Kesif_Form, YourSecondForm # Import your second form class Anasayfa_View(TemplateView): template_name = 'index.html' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['kform'] = Kesif_Form(prefix='kesif') context['second_form'] = YourSecondForm(prefix='second') return context
Template for two forms:
<!-- First Form --> <form action="{% url 'kesif' %}" method="POST"> {% csrf_token %} {{ kform.errors }} {{ kform }} <input type="submit" value="Keşif Talebi Gönder"> </form> <!-- Second Form --> <form action="{% url 'second_form_submit' %}" method="POST"> {% csrf_token %} {{ second_form.errors }} {{ second_form }} <input type="submit" value="İkinci Form Gönder"> </form>
Corresponding view for the second form:
def second_form_submit(request): if request.method == 'POST': form = YourSecondForm(request.POST, prefix='second') if form.is_valid(): form.save() return redirect('grsndx') # Redirect back to homepage if GET request or form invalid return redirect('grsndx')
Quick Final Checks
- Fix your
tarihfield: Settingdefault=Nonecan cause validation errors if the form doesn't receive a datetime value. Useauto_now_add=Trueto automatically set it to the current time when a record is created:tarih = models.DateTimeField(auto_now_add=True, verbose_name="Tarih") - Use your browser's developer tools (Network tab) when submitting the form: Look for 403 Forbidden errors (sign of missing CSRF token) or 500 Internal Server Errors (check Django's server logs for specific error details).
内容的提问来源于stack exchange,提问作者ZeQ

