【发布时间】:2022-06-15 23:31:43
【问题描述】:
我早些时候尝试寻找答案,但似乎无法弄清楚一些事情。我在 form.py 文件中创建我的表单,所以它是一个 python 文件。
这是我的 forms.py 文件:
class UploadForm(ModelForm):
name = forms.TextInput(attrs={'class': 'myfieldclass'})
details = forms.TextInput()
littype = forms.TextInput()
image = forms.ImageField()
class Meta:
model = info
fields = ["name", "details", "littype", "image"]
这是我的views.py函数,如果它有助于找到解决方案:
def uploadform(request):
if request.method == 'POST':
form = UploadForm(request.POST, request.FILES)
print(request.FILES)
if form.is_valid():
form.save()
redirect(home)
return render(request, 'uploadform.html', {'form': UploadForm})
为了给它设置样式,我想我可以做这样的事情,我在另一个问题中找到了:
class MyForm(forms.Form):
myfield = forms.CharField(widget=forms.TextInput(attrs={'class': 'myfieldclass'}))
除了我不知道如何将 css 页面链接到该 python 文件。这是我尝试编写的,但我认为它不起作用,因为它适用于 html,但它位于 python 文件中:
<link type="text/css" rel="stylesheet" href="templates/form.css">
所以我只是不确定如何设置我的表单样式。感谢您的任何回答!
【问题讨论】:
-
您不需要将 css 链接到 python 文件,CSS 应该链接到父文件,
myfield = forms.CharField(widget=forms.TextInput(attrs={'class': 'myfieldclass'}))将生成一个具有类 'myfieldclass' 的字段,该字段将从父文件 -
我对其中一些术语有点陌生,因为我编码的时间太长了,所以如果您解释父文件的含义是非常有帮助的,因为我知道只有一个文件夹可以包含一个文件做不到的其他文件。
标签: python html django forms styles