Auto pre-fill form Django

huangapple go评论92阅读模式
英文:

Auto pre-fill form Django

问题

To pre-fill form fields in Django using the initial parameter, you can modify your OrderCreateForm in your Django forms.py like this:

  1. class OrderCreateForm(forms.ModelForm):
  2. class Meta:
  3. model = Order
  4. fields = ['first_name', 'last_name', 'email', 'address', 'postal_code', 'city']
  5. def __init__(self, *args, **kwargs):
  6. super(OrderCreateForm, self).__init__(*args, **kwargs)
  7. if self.instance:
  8. # Pre-fill the form fields with data from the User class
  9. self.fields['first_name'].initial = self.instance.user.first_name
  10. self.fields['last_name'].initial = self.instance.user.last_name
  11. self.fields['email'].initial = self.instance.user.email

This code ensures that the first_name, last_name, and email fields in your order form are pre-filled with the corresponding data from the User class when forming the order form.

英文:

How to pre-fill form fields in Django. What would be the pre-filled data from the User class when forming the order form.

The result need like this:

my view

  1. def order_create(request):
  2. cart = Cart(request)
  3. user = request.user
  4. if request.user.is_authenticated:
  5. if request.method == 'POST':
  6. form = OrderCreateForm(request.POST)
  7. if form.is_valid():
  8. order = form.save(commit=False)
  9. if cart.coupon:
  10. order.coupon = cart.coupon
  11. order.discount = cart.coupon.discount
  12. order.save()
  13. for item in cart:
  14. OrderItem.objects.create(order=order,
  15. product=item['product'],
  16. price=item['price'],
  17. quantity=item['quantity'])
  18. cart.clear()
  19. order_created.delay(order.id)
  20. request.session['order_id'] = order.id
  21. return redirect(reverse('payment:process'))
  22. else:
  23. form = OrderCreateForm()
  24. else:
  25. return redirect('login')
  26. return render(request,
  27. 'orders/order/create.html',
  28. {'cart': cart, 'form': form})

my form

  1. class OrderCreateForm(forms.ModelForm):
  2. class Meta:
  3. model = Order
  4. fields = ['first_name', 'last_name', 'email',
  5. 'address', 'postal_code', 'city']

my teplate

  1. <form action="." method="post" class="order-form">
  2. {{ form.as_p }}
  3. <p><input type="submit" value="Place order"></p>
  4. {% csrf_token %}
  5. </form>

Im try to use initial but with no result.

答案1

得分: 0

通过在创建表单实例时使用 initial 参数来传递“预填充”数据。

  1. form = OrderCreateForm(initial={
  2. 'first_name': user.first_name,
  3. 'email': user.email,
  4. })
英文:

Pass your "pre-fill" data using the initial argument when creating your form instance

  1. form = OrderCreateForm(initial={
  2. 'first_name': user.first_name,
  3. 'email': user.email,
  4. })

huangapple
  • 本文由 发表于 2023年3月31日 04:36:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定