gpt4 book ai didi

python - Django 1.10 AdminDateWidget() 在ModelForm中的使用

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:31 26 4
gpt4 key购买 nike

Django 文档并不清楚如何使用 ModelForm。首先,如何设置我的 urls.py?

我只是这样做:

from . import views as music_views
url(r'album/add/$', music_views.AlbumCreate(), name='album-add'),

在我看来,我正在尝试使用 AdminDateWidget:

from . import models as music_models
from django import forms
from django.contrib.admin.widgets import AdminDateWidget
class AlbumCreate(forms.ModelForm):
class Meta:
releasedate = forms.DateField(widget=AdminDateWidget())
model = music_models.Album
fields = ['artist', 'album_title', 'genre', releasedate, 'notes', 'album_logo', 'rating']

我的模板是:

{% for field in form %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<span class="text-danger small">{{ field.errors }}</span>
</div>
<label class="control-label col-sm-2">
{{ field.label_tag }}
</label>
<div class="col-sm-10">
{{ field }}
</div>
</div>
{% endfor %}

我在这里遇到的问题是在呈现页面时出现此错误:

Exception Value:    
sequence item 0: expected str instance, DateField found

但是根据https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/ docs,DateField 是允许的数据类型之一。所以我显然以某种方式做错了,但不知道如何做对。有人可以帮我解决这个问题吗?如果可能的话,我倾向于从基础示例中学习。

最佳答案

乍一看,似乎有很多问题(模板中没有表单标签,字段只能接受字符串等)但最重要的是对于那些想使用 AdminDateWidget 的人:您需要加载许多额外的脚本,这些脚本不包含在通常的 {{ form.media }} 中。

project/urls.py

from django.conf.urls import include, url
from album.views import album_add


urlpatterns = [
url(r'album/add/$', album_add, name='album-add'),
]

album/models.py

from django import forms
from django.db import models
from django.contrib.admin.widgets import AdminDateWidget


class Album(models.Model):
artist = models.CharField(max_length=30)
releasedate = models.DateField()


class AlbumForm(forms.ModelForm):
releasedate = forms.DateField(widget=AdminDateWidget)

class Meta:
model = Album
fields = ['artist', 'releasedate']

album/views.py

from .models import AlbumForm
from django.shortcuts import render, HttpResponse


def album_add(request):
if request.method == "GET":
form = AlbumForm()
return render(request, 'album/create_album.html', {'form': form})
elif request.method == "POST":
form = AlbumForm(request.POST)
if form.is_valid():
form.save()
return HttpResponse("Saved.")
return render(request, 'album/create_album.html', {'form': form})

album/templates/album/create_album.html

{% load static  %}

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="{% static 'admin/js/core.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/vendor/jquery/jquery.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/jquery.init.js' %}"></script>

{{ form.media }}

<link rel="stylesheet" type="text/css" href="{% static 'admin/css/base.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/widgets.css' %}" />

<form>
{% csrf_token %}
<table>
{{ form }}
</table>
<input type="submit" value="Go!">
</form>

我希望这对某人有帮助!

关于python - Django 1.10 AdminDateWidget() 在ModelForm中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40204020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com