[英]Setting initial field value in generic form through URL parameter
I'm struggling to set an initial value in a form instance based on the URL parameter in Django 3.0.我正在努力根据 Django 3.0 中的 URL 参数在表单实例中设置初始值。
I have a Claim
model:我有一个
Claim
model:
# models.py
class Claim(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
text = models.TextField()
date_added = models.DateTimeField(default=timezone.now)
member = models.ForeignKey(User, on_delete=models.CASCADE)
I have a NewClaimForm
based on ModelForm:我有一个基于 ModelForm 的
NewClaimForm
:
# forms.py
class NewClaimForm(forms.ModelForm):
class Meta:
model = Claim
fields = ['product', 'text']
I have a NewClaimView
based on CreateView:我有一个基于 CreateView 的
NewClaimView
:
# views.py
class NewClaimView(LoginRequiredMixin, CreateView):
model = Claim
form_class = NewClaimForm
template_name = 'portal/new_claim.html'
def form_valid(self, form):
form.instance.member = self.request.user
return super(NewClaimView, self).form_valid(form)
And using the following template fragment on the index page...并在索引页面上使用以下模板片段......
# index.html
<div class="card-deck">
{% for product in products %}
<div class="card text-center">
<div class="card-header">
<h5 class="card-title text-primary">{{ product }}</h5>
</div>
<div class="card-body">
<ol class="card-text text-left">
<li>Fill in the {{ product }} form</li>
<li>Attach your medical records</li>
<li>Get your claim reviewed within 48 hours</li>
</ol>
<a href="{% url 'portal:new_claim_product' product_id=product.id %}" class="btn btn-primary">Online Form</a>
</div>
</div>
{% endfor %}
</div>
...I pass the product_id parameter to the URL: ...我将 product_id 参数传递给 URL:
# urls.py
app_name = 'portal'
urlpatterns = [
path('new_claim/<int:product_id>/', NewClaimView.as_view(), name='new_claim_product'),
]
And lastly, this is what my new_claim
template looks like:最后,这是我的
new_claim
模板的样子:
# new_claim.html
{% extends "portal/base.html" %}
{% load bootstrap4 %}
{% block content %}
<p>Submit a new claim</p>
<form action="{% url 'portal:new_claim' %}" method='post' class="form" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button name="submit">Submit claim</button>
{% endbuttons %}
</form>
{% endblock content %}
I would like to now use the product_id
to set the initial value of the product
field in the form instance according to product_id
.我现在想使用
product_id
根据product_id
设置表单实例中product
字段的初始值。 How can I achieve this?我怎样才能做到这一点?
Figured out how to do this by modifying the get method for my class-based view:通过修改我的基于类的视图的 get 方法来弄清楚如何做到这一点:
# views.py
class NewClaimView(LoginRequiredMixin, CreateView):
model = Claim
form_class = NewClaimForm
template_name = 'portal/new_claim.html'
def get(self, request, product_id=None, *args, **kwargs):
form = self.form_class(initial={'product': product_id})
return render(request, self.template_name, {'form': form})
def form_valid(self, form):
form.instance.member = self.request.user
return super(NewClaimView, self).form_valid(form)
Here's a link to the relevant documentation .这是相关文档的链接。
Is this an optimal way to solve this?这是解决此问题的最佳方法吗?
Do I need the *args and **kwargs in the modified get method?我需要修改后的 get 方法中的 *args 和 **kwargs 吗? I'm not using them in my code but perhaps it would be useful to keep them there for other purposes in the future?
我没有在我的代码中使用它们,但将来将它们保留在那里用于其他目的可能会很有用?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.