You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django 1.11详情页关联Event与Register模型提交无数据问题

Fixing Registration Data Not Saving in Django 1.11 Event Detail View

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 the post method to make sure we have access to the specific Event the user is viewing.
  • In form_valid, we first save the registration with commit=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

  1. Restart your Django server after applying these changes.
  2. Navigate to an Event detail page, fill out the form, and submit.
  3. Check your Django admin panel—you should see a new Register entry 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:49:34