Django 1.11详情页关联Event与Register模型提交无数据问题
Hey there! Let's break down why your Register model isn't getting new entries when users submit the form, and fix it step by step.
1. The Core Issue: Your View Isn't Saving the Form Data
You're using DetailView with FormMixin, but FormMixin's default form_valid method doesn't save the form—it just handles the redirect. That's why no data is being added to your Register table. You need to override this method to actually save the form and link it to the current Event.
Updated View Code
class EventDetailView(DetailView, FormMixin): model = models.Event form_class = forms.RegisterForm def get_success_url(self): return reverse('events:list') def post(self, request, *args, **kwargs): self.object = self.get_object() # Ensure we have the current Event instance form = self.get_form() if form.is_valid(): return self.form_valid(form) else: return self.form_invalid(form) def form_valid(self, form): # Save registration without committing to DB immediately register_instance = form.save(commit=False) register_instance.save() # Link the current Event to the new registration register_instance.event.add(self.object) return super().form_valid(form)
- We added
self.object = self.get_object()in thepostmethod to make sure we have access to the specific Event the user is viewing. - In
form_valid, we first save the registration withcommit=False(to avoid an immediate database save), then explicitly associate the current Event with the new registration via the ManyToMany field, before finalizing the save.
2. Fix a Critical Model Bug: phone_number Field
Your Register model has an invalid field definition that can cause silent validation failures:
phone_number = models.IntegerField(max_length=30) # Incorrect!
IntegerField does not support the max_length parameter, and phone numbers should never be stored as integers (they often start with 0, include symbols like + or -, or exceed integer value limits). Replace it with a CharField:
phone_number = models.CharField(max_length=30)
This fixes validation issues that might be stopping your form from submitting successfully without any visible error.
3. Verify Your Form Setup
Make sure your RegisterForm excludes the event field (since we're handling that association in the view, not letting users select it manually):
class RegisterForm(forms.ModelForm): class Meta: model = Register exclude = ['event'] # Or list all required fields explicitly, e.g., fields = ['first_name', 'last_name', 'email', ...]
Including event in the form would force users to pick an Event, which defeats the purpose of linking to the current Event automatically.
Testing Tips
- Restart your Django server after applying these changes.
- Navigate to an Event detail page, fill out the form, and submit.
- Check your Django admin panel—you should see a new
Registerentry linked to the correct Event.
If you still run into problems, check your server logs for hidden validation errors (like missing required fields or invalid email formats) that might be causing the form to fail silently.
内容的提问来源于stack exchange,提问作者user9192656

