我试图将“活动”外键字段(它是html形式的选择选项字段)设置为从url传递的整数的初始值。 campaign_id值完美地达到了我编写的视图的get_initial方法,但即使我从urls.py传递的campaign_id存在于html表单中,但html表单仍未选择该值且未设置选择选项字段广告系列表。
#models.py
class CampaignDetails(Timestamp):
compaign = models.ForeignKey(Campaign, on_delete=models.CASCADE)
caption_example = models.TextField(max_length=500)
#url.py
path('create/details/<int:campaign_id>/', CampaignCreateDetails.as_view(), name='create-details'),
#views.py
class CampaignCreateDetails(CreateView):
model = CampaignDetails
fields = '__all__'
template_name = 'campaign/campaign_details_create.html'
success_url = '/brand-list/'
def get_initial(self):
return {
'campaign': self.kwargs.get('campaign_id')
}
# campaign/campaign_details_create.html
<form method="post" enctype="multipart/form-data">{% csrf_token %}
{{form.errors}}
{{form.as_p}}
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
<i class="material-icons right">send</i>
</button>
</form>