Django 模型表单不生成预期的 HTML

发布于 2024-10-06 05:54:16 字数 1031 浏览 0 评论 0原文

我有一个模型表单,它不会生成(一些)代表模型字段的 HTML。正如您从底部的输出中看到的,它只是输出一个空行,应该在其中输出标题,并且可能是文件字段的浏览按钮(或其他东西 - 对吗?)。

#views.py
def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
    else:
        form = UploadFileForm()
    return render_to_response('files/upload_file.html', { 'form': form })

#models.py
from django import forms
from django.db import models
from django.forms import ModelForm

class UploadFile(models.Model):
    title = forms.CharField(max_length = 50)
    theFile = forms.FileField()

    def __unicode__(self):
        return str(title)

class UploadFileForm(ModelForm):
    class Meta:
        model = UploadFile

#upload_file.html
<form action="" method="POST" enctype="multipart/form-data">
    {{ form }}
    <input type="submit" value="Upload File">
</form>

#The HTML output
<form action="" method="POST" enctype="multipart/form-data">

    <input type="submit" value="Upload File">
</form>

I have a modelform that doesn't produce (some of) the HTML that (should) represent the model fields. As you can see from the output at the bottom, it's just outputting a blank line where it should be outputting the title and presumably a browse button (or something--right?) for the filefield.

#views.py
def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
    else:
        form = UploadFileForm()
    return render_to_response('files/upload_file.html', { 'form': form })

#models.py
from django import forms
from django.db import models
from django.forms import ModelForm

class UploadFile(models.Model):
    title = forms.CharField(max_length = 50)
    theFile = forms.FileField()

    def __unicode__(self):
        return str(title)

class UploadFileForm(ModelForm):
    class Meta:
        model = UploadFile

#upload_file.html
<form action="" method="POST" enctype="multipart/form-data">
    {{ form }}
    <input type="submit" value="Upload File">
</form>

#The HTML output
<form action="" method="POST" enctype="multipart/form-data">

    <input type="submit" value="Upload File">
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美男兮 2024-10-13 05:54:16

实际上,再想一想,您的错误在于声明您的模型 UploadFile (由于缺乏响应而经过双重检查)。您应该使用 models.CharField 而不是 forms.CharField。因此你的模型没有任何内容;因此 ModelForm 没有任何字段(它不会作为错误出现,以防某些高级用户想要将您最终想要使用的表单字段附加到模型)。您还需要为 FileField 指定 upload_to 位置,主要是您的媒体目录。

class UploadFile(models.Model):
    title = models.CharField(max_length = 50)
    theFile = models.FileField(upload_to="files/")

    def __unicode__(self):
        return str(title)

Actually on second thought your mistake is in declaring your model UploadFile (after doublechecking due to lack of a response). You are supposed to use models.CharField not forms.CharField. Thus your model doesn't have any content; hence the ModelForm doesn't have any fields (it didn't arise as an error just in case some advanced user wanted to attach the form fields you eventually wanted to use to a model). You also will need to give the FileField an upload_to location, principally your media directory.

class UploadFile(models.Model):
    title = models.CharField(max_length = 50)
    theFile = models.FileField(upload_to="files/")

    def __unicode__(self):
        return str(title)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文