亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用從變量中獲得的默認值自動填充模型表單字段?

如何使用從變量中獲得的默認值自動填充模型表單字段?

慕虎7371278 2022-07-05 16:54:37
    我正在使用 ModelForm,我想用一個初始值填充我的 dataset_id 字段,該初始值將來自爭論中傳遞的“pid”變量。我已經嘗試了一些東西并附加了該代碼,但它不起作用views.pydef home(request, pid):    # if this is a POST request we need to process the form data    if request.method == 'POST':        # create a form instance and populate it with data from the request:        form = DelegateForm(request.POST)        # check whether it's valid:        if form.is_valid():            # process the data in form.cleaned_data as required            p = form.save()            # redirect to a new URL:            return HttpResponseRedirect('/')        # if a GET (or any other method) we'll create a blank form    else:         # Here I want to pass the value of "pid" to the dataset_id field so that when it renders it is populated with the value in the pid variable        form = DelegateForm(initial={'dataset_id': pid})    return render(request, 'add_delegate.html', {'dform': form, 'uid': pid})models.pyclass Delegate(models.Model):    dataset_id = models.CharField(max_length=255, null=True, blank=True)    first_name = models.CharField(max_length=255)    last_name = models.CharField(max_length=255, null=True, blank=True)    email = models.CharField(max_length=255, null=True, blank=True)    phone = models.IntegerField(null=True, blank=True)    company = models.CharField(max_length=255, null=True, blank=True)    designation = models.CharField(max_length=255, null=True, blank=True)    address = models.CharField(max_length=255, null=True, blank=True)    city = models.CharField(max_length=255, null=True, blank=True)    pincode = models.IntegerField(null=True, blank=True)    image_path = models.CharField(max_length=2083, null=True, blank=True)forms.py
查看完整描述

2 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

我的代碼中有一些功能沖突,我將它們分開,所以現在我有了解決方案



查看完整回答
反對 回復 2022-07-05
?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

我想應該在url的簽名中使用pid作為參數,以便將其傳遞給相應的視圖(home本例中的視圖)。您的 urls 文件將包含

path('home_path/<int:pid>', views.home, name='a')

而不是在視圖文件中:

def home(request, pid):
    ...


查看完整回答
反對 回復 2022-07-05
  • 2 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號