Django应用部署:评分表单重定向异常及模型保存失败问题
Hey there! Let's figure out why your rating form is behaving unexpectedly while your contact form works fine. This sounds like a couple of common Django form pitfalls—let's break down the fixes step by step.
1. Why It's Redirecting to /rating/ Instead of Home
First off, the redirect issue is almost certainly tied to your form's action attribute in the template. If your rating form is pointing to /rating/ instead of your homepage URL, that's exactly where it'll send the POST request.
Check your template code for the rating form:
<!-- ❌ Wrong: Hardcodes or points to the wrong URL --> <form method="POST" action="/rating/"> {% csrf_token %} <!-- form fields --> </form> <!-- ✅ Correct: Points to your homepage (use the URL name for portability) --> <form method="POST" action="{% url 'home' %}"> {% csrf_token %} {{ rating_form.as_p }} <button type="submit" name="rating_submit">Submit Rating</button> </form>
If you leave the action empty, it'll default to the current page (your homepage), which works too—just make sure you're not explicitly setting it to /rating/.
2. Why First Submission Fails to Save the Model
This is likely a combination of two issues: not properly distinguishing between the two forms on your homepage, and not handling form validation correctly in your view.
Fix Your View Logic
Since both forms live on the same page, your view needs to know which one was submitted. Use unique name attributes on your submit buttons (like rating_submit above) to tell them apart. Here's a corrected view structure:
from django.shortcuts import render, redirect from .forms import RatingForm, ContactForm def home(request): # Initialize empty forms for GET requests rating_form = RatingForm() contact_form = ContactForm() if request.method == 'POST': # Handle Rating Form submission if 'rating_submit' in request.POST: rating_form = RatingForm(request.POST) if rating_form.is_valid(): rating_form.save() # Always redirect after successful POST to avoid duplicate submissions return redirect('home') # If invalid, the form will carry errors to render on the homepage # Handle Contact Form submission elif 'contact_submit' in request.POST: contact_form = ContactForm(request.POST) if contact_form.is_valid(): contact_form.save() return redirect('home') # Render homepage with forms (empty or with errors) return render(request, 'home.html', { 'rating_form': rating_form, 'contact_form': contact_form })
Key Fixes Here:
- Bind POST data only to the relevant form: No more empty forms trying to validate POST data, which was likely causing the first submission failure.
- Redirect after successful save: This ensures users land back on the homepage and prevents duplicate submissions if they refresh the page.
- Pass invalid forms back to the template: Users will see clear validation errors (like missing fields) instead of silent failures.
3. Quick Debugging Steps
If you're still stuck, add these checks to pinpoint the issue:
- Print form errors in your view to spot validation issues:
if not rating_form.is_valid(): print("Rating Form Errors:", rating_form.errors) - Verify your
Ratingmodel doesn't have customsave()logic that blocks the first save (e.g., a required field that's not being set properly). - Double-check that your rating form includes the
{% csrf_token %}tag—though your contact form works, it's worth confirming this isn't missing.
That should cover the main issues! Let me know if you hit any snags while implementing these fixes.
内容的提问来源于stack exchange,提问作者Aryan Curiel

