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

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

Django表單未出現在模板上

Django表單未出現在模板上

一只萌萌小番薯 2022-09-20 16:45:40
我目前正在從事一個需要Django Forms的項目,但我最終遇到了一些問題。我的表單根本不顯示...我的模板上未顯示任何字段。所以我的代碼:models.pyclass Place(models.Model):    name = models.CharField(max_length=255)    longitudeMax = models.DecimalField(max_digits=8, decimal_places = 4 ,blank=True)    longitudeMin = models.DecimalField(max_digits=8, decimal_places = 4, blank=True)    latitudeMax = models.DecimalField(max_digits=8, decimal_places = 4, blank=True)    latitudeMin = models.DecimalField(max_digits=8, decimal_places = 4, blank=True)    datasetPath = models.CharField(max_length=255)    isActive = models.BooleanField(default=True)    def __str__(self):        return self.name    def get_place(self, name):        return Noneforms.pyclass NewPlaceForm(forms.Form):    name = forms.CharField(        widget=forms.TextInput(            attrs={                "placeholder" : "Name",                                "class": "form-control"            }        ))    longMax = forms.DecimalField(        widget=forms.NumberInput(            attrs={                "placeholder" : "Longitude Max",                                "class": "form-control"            }        ))    longMin = forms.DecimalField(        widget=forms.NumberInput(            attrs={                "placeholder" : "Longitude Min",                                "class": "form-control"            }        ))    latMax = forms.DecimalField(        widget=forms.NumberInput(            attrs={                "placeholder" : "Latitude Max",                                "class": "form-control"            }        ))    latMin = forms.DecimalField(        widget=forms.NumberInput(            attrs={                "placeholder" : "Latitude Min",                                "class": "form-control"            }        ))    class Meta:        model = Place        fields = ('name', 'longitudeMax', 'longitudeMin', 'latitudeMax', 'latitudeMin')]
查看完整描述

1 回答

?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

Django處理開箱即用的表單呈現,要顯示基本表單,請嘗試:


  <form method="POST" action="{% url 'namespaced:url' %}">

    { csrf_token %}

    {{ form.as_p }}

    <input type="submit" value="submit">

  </form>

也就是說,看起來您正在模板中使用引導,您可能會發現該包很有用(https://github.com/zostera/django-bootstrap4)。django-bootstrap4


快速示例:


pip install django-bootstrap4


并添加到已安裝的應用。bootstrap4


然后在您的模板中:


{% load bootstrap4 %}


{# Load CSS and JavaScript #}

{% bootstrap_css %}

{% bootstrap_javascript jquery='full' %}




<form action="{% url 'namespaced:url' %}" method="post" class="form">

  {% csrf_token %}

  {% bootstrap_form form %}

  {% buttons %}

    <button type="submit" class="btn btn-primary">

      Submit

    </button>

  {% endbuttons %}

</form>

如果需要,可以使用: 手動呈現每個字段,因此您可以執行類似 之類的操作來手動排列頁面上的表單字段。{% bootstrap_field field %}<div class="col-md-6">{% bootstrap_field my_field %}</div>


查看完整回答
反對 回復 2022-09-20
  • 1 回答
  • 0 關注
  • 80 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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