我早些時候嘗試尋找答案,但似乎無法弄清楚一些事情。我在 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">
所以我只是不確定如何設定我的表單樣式。感謝您的任何回答!
uj5u.com熱心網友回復:
在您的模板中,您只需將css檔案匯入.head tagload static
.html 檔案:
{% load static %}
<!doctype html>
<html lang="en">
<head>
# import the css file here
<link rel="stylesheet" href="{% static 'path to css file' %}">
</head>
...
</html>
在 css 檔案中:
# Styling the class
.myfieldclass{
width: 30% !important;
height: 100px !important;
...
}
但請注意,您不必匯入 css 檔案,因為您也可以在 head 標簽中添加樣式標簽。例如:
<!doctype html>
<html lang="en">
<head>
<style type="text/css">
.myfieldclass{
width: 30% !important;
height: 100px !important;
...
}
</style>
</head>
...
</html>
您也可以css從python檔案本身申請。
從python檔案的方法。
# Custom way to set css styling on a form field in python code
def field_style():
styles_string = ' '
# List of what you want to add to style the field
styles_list = [
'width: 30% !important;',
'height: 100px !important;',
]
# Converting the list to a string
styles_string = styles_string.join(styles_list)
# or
# styles_string = ' '.join(styles_list)
return styles_string
class MyForm(forms.Form):
myfield = forms.CharField(widget=forms.TextInput(attrs={'class': 'myfieldclass', 'style': field_style()}))
# 'style': field_style() .... field_style() will return in this case 'width: 30% !important; height: 100px !important;'
其中任何一個都應該作業,但我確實建議從hmtlorcss檔案中進行樣式設定。
uj5u.com熱心網友回復:
class StocksForm(forms.Form):
stocks = forms.CharField(
label="",
widget=forms.TextInput(
attrs={
"class": "special",
"size": "40",
"label": "comment",
"placeholder": "Comma Seperated eg - Reliance, Steel, Acrysil.. ",
}
),
)
模板面
{% load static %}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="{% static '/css/cards.css' %}">
樣式表的 CSS 為
.special{
font-variant: small-caps;
}
這就是 django 使用類渲染表單的方式,樣式類將從樣式表中獲取

希望這可以幫助!:)
uj5u.com熱心網友回復:
您可以像任何其他樣式表一樣在模板標題中包含 CSS
謝謝
uj5u.com熱心網友回復:
我的標準模板使用 Django 塊結構,因此我可以將特定于頁面的 CSS 與其頁面分組。
{% extends "base.html" %}
{% block content %}
my html ...
{% endblock %}
{% block extracss %}
{{block.super}}
<style>
... page-specific CSS
</style>
{% endblock %}
Base.html 包含一個空塊定義
{% block extracss %}{% endblock %}
它用于使用其他站點范圍的 CSS 定位塊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495416.html
下一篇:單擊其他位置后保持專注于輸入文本
