Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向模型表单django添加自定义CSS样式_Python_Css_Django - Fatal编程技术网

Python 向模型表单django添加自定义CSS样式

Python 向模型表单django添加自定义CSS样式,python,css,django,Python,Css,Django,我正在使用一个引导变量来帮助设计模型表单的样式。有一个特定的类,我希望其中一个字段是这样的,我已经阅读了关于这个主题的内容,普遍的共识是向模型表单的元添加一个小部件,就像我在下面尝试的那样: forms.py class EmailForm(forms.ModelForm): class Meta: model = MarketingEmails fields = ['messageid','subject','body','name','altsubje

我正在使用一个引导变量来帮助设计模型表单的样式。有一个特定的类,我希望其中一个字段是这样的,我已经阅读了关于这个主题的内容,普遍的共识是向模型表单的元添加一个小部件,就像我在下面尝试的那样:

forms.py

class EmailForm(forms.ModelForm):
    class Meta:
        model = MarketingEmails
        fields = ['messageid','subject','body','name','altsubject','utm_source','utm_content','utm_campaign',]
        widgets = {
            'body': Textarea(attrs={'class': 'summernote'}),
        }
class EmailForm(forms.ModelForm):
    ...
    subject = forms.CharField(
        label = 'Subject',
        max_length = 1000,
        required = True,
        widget = forms.TextInput(
            attrs = {'class': 'summernote', 'name': 'subject'}
        )
    )   

    body = forms.CharField(
        label = 'Body',
        max_length = 1000,
        required = True,
        widget = forms.TextInput(
            attrs = {'class': 'summernote', 'name': 'body'}
        )
    )   
    ...

    class Meta:
        model = MarketingEmails
        fields = ('messageid','subject','body','name', ... )
但是,这似乎没有呈现到我的模板上,即:

<div class="row">
<div class="col-sm-6">
    <form method="POST" class="post-form" action ="">
    {% csrf_token %}        
        <p><label for="id_subject">Subject</label>
        <input class="form-control" id="id_subject" type="text" name="subject" maxlength="1000" value="{{rows.subject}}"required /></p>

        <p><label for="id_name">Name</label>
        <input class="form-control" id="id_name" type="text" name="name" maxlength="1000" value="{{rows.name}}"required /></p>

        <p><label for="id_body">Body</label>
        <input class="form-control" id="id_body" type="text" name="body" maxlength="1000" value="{{rows.body}}"required /></p>

        <p><label for="id_altsubject">Alt Subject</label>
        <input class="form-control" id="id_altsubject" type="text" name="altsubject" maxlength="1000" value="{{rows.altsubject}}"required /></p>

        <p><label for="id_utm_source">utm_source</label>
        <input class="form-control" id="id_utm_source" type="text" name="utm_source" maxlength="1000" value="{{rows.utm_source}}"required /></p>

        <p><label for="id_utm_content">utm_content</label>
        <input class="form-control" id="id_utm_content" type="text" name="utm_content" maxlength="1000" value="{{rows.utm_content}}"required /></p>

        <p><label for="id_utm_campaign">utm_campaign</label>
        <input class="form-control" id="id_utm_campaign" type="text" name="utm_campaign" maxlength="1000" value="{{rows.utm_campaign}}"required /></p>

        <button type="submit" class="save btn btn-default">Save</button>

    </form>
</div>
views.py:

def emailinfo(request, pk):
if request.session.has_key('shortname'):
    shortname =  request.session['shortname']
    form = MarketingEmails.objects.filter(messageid =pk).get()
    if request.method == 'GET':
        form = EmailForm(instance=form)
        return render(request, 'marketingemails/emailinfo.html',{'shortname': shortname, 'form': form})

    else:
        form = EmailForm(request.POST,instance=form)
        if form.is_valid():
            return redirect('marketingemails:emailinfo', pk = form.messageid)

    return render(request, 'marketingemails/emailinfo.html',{'shortname': shortname, 'form': form})
else:
    return HttpResponseRedirect(reverse('common:login'))    
html:


{%csrf_令牌%}
{%形式的字段为%}
{{field.label_tag}
{{field}}
{%if field.help_text%}
{{field.help_text}
{%endif%}
{%字段中有错误。错误%}
{{error}}
{%endfor%}
{%endfor%}
提交

在我使用的模板中,您可以添加CSS类或twitter引导类。它真的很有用

<form method='POST' action="/" enctype='multipart/form-data'>
 {% load widget_tweaks %}
 {% csrf_token %}
 {{ form.first_name |add_class:"customCSS1 customCSS2" }}
 {{ form.second_name |add_class:"form-control customCSS4" }}
</form>
{{ form.media.js }}
试试这个:

forms.py

class EmailForm(forms.ModelForm):
    class Meta:
        model = MarketingEmails
        fields = ['messageid','subject','body','name','altsubject','utm_source','utm_content','utm_campaign',]
        widgets = {
            'body': Textarea(attrs={'class': 'summernote'}),
        }
class EmailForm(forms.ModelForm):
    ...
    subject = forms.CharField(
        label = 'Subject',
        max_length = 1000,
        required = True,
        widget = forms.TextInput(
            attrs = {'class': 'summernote', 'name': 'subject'}
        )
    )   

    body = forms.CharField(
        label = 'Body',
        max_length = 1000,
        required = True,
        widget = forms.TextInput(
            attrs = {'class': 'summernote', 'name': 'body'}
        )
    )   
    ...

    class Meta:
        model = MarketingEmails
        fields = ('messageid','subject','body','name', ... )
view.py

from django.shortcuts import render
from your_app_path.forms import EmailForm

def fname(request):
    ...
    marketing = MarketingEmails.objects.get(...)

    form = EmailForm(instance=marketing) 
    ...

    return render(request, 'yourview.html', { 'form': form })
yourview.html

<form action="" method="post">
  {% csrf_token %}
  {% for field in form %}
    {{ field.label_tag }}
    {{ field }}

    {% if field.help_text %}
      {{ field.help_text }}
    {% endif %}

    {% for error in field.errors %}
      {{ error }}
    {% endfor %}

  {% endfor %}
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

{%csrf_令牌%}
{%形式的字段为%}
{{field.label_tag}
{{field}}
{%if field.help_text%}
{{field.help_text}
{%endif%}
{%字段中有错误。错误%}
{{error}}
{%endfor%}
{%endfor%}
提交

我已按建议设置了代码,现在它已设置样式,但信息不会像以前那样显示在网页上,我已在代码上添加了更新-您知道这是为什么吗?
<form action="" method="post">
  {% csrf_token %}
  {% for field in form %}
    {{ field.label_tag }}
    {{ field }}

    {% if field.help_text %}
      {{ field.help_text }}
    {% endif %}

    {% for error in field.errors %}
      {{ error }}
    {% endfor %}

  {% endfor %}
  <button type="submit" class="btn btn-primary">Submit</button>
</form>