【问题标题】:Django input forms not showing up in my templateDjango 输入表单未显示在我的模板中
【发布时间】:2021-04-01 08:02:24
【问题描述】:

我的问题是,当我转到我的 add.html 页面时,标题和文本区域的输入表单不显示。对于上下文,这是针对我必须设计“wiki”的在线课程。这部分代码用于添加条目(标题和正文内容)

这是我的代码:

views.py

class Form(forms.Form):
    title = forms.CharField(label="Post Title")
    textarea = forms.CharField(widget=forms.Textarea(), label='')

def create(request):
    if request.method == "POST":
        form = Form(request.POST)
        if form.is_valid():
            title = form.cleaned_data["title"]
            textarea = form.cleaned_data["textarea"]
            entries = util.list_entries()
            if title in entries:
                return render(request, "encyclopedia/error.html")
            else:
                util.save_entry(title, textarea)
                page = util.get_entry(title)
                page_converted = markdowner.convert(page)

                context = dict(body=page_converted, title=title)

                return render(request, "encyclopedia/entry.html", context)
    else:
        return render(request, "encyclopedia/create.html", {
            "post": Form()
        })

add.html

{% extends "encyclopedia/layout.html" %}

{% block body %}
<h1>
    Add Post
</h1>
    <form action="{% url 'create' %}" method="post">
        {% csrf_token %}
        <h6 class="post-title">Title: {{ post.title }}</h6>
            {{ post.textarea }}
        <input type="submit" value="add entry">
    </form>
    <a href="{% url 'index' %}">View Entries</a>
{% endblock %}

【问题讨论】:

    标签: python html django web


    【解决方案1】:

    尝试使用这个:-

    add.html

     {% extends "encyclopedia/layout.html" %}
    
     {% block body %}
    
    <h2>Add Post</h2>
    <div class="container">
        <form method="post" enctype="multipart/form-data">
            {% csrf_token %}
            <table>
            {{ form.as_p }}
            </table>
            <button type="submit">Add</button>
        </form>
    </div>
    <a href="{% url 'index' %}">View Entries</a>
    
    {% endblock body %}
    

    views.py

    def create(request):
    
        if request.method != 'POST':
    
            form = Form()
        else:
    
            form = Form(data=request.POST)
            new_post = form.save(commit=False)
            new_post.owner = request.user
            new_post.save()
            return redirect('/') #put here, whatever and whenever you want to redirect.
    
    
        context = {'form':form}
        return render(request, 'mains/create.html', context)
    

    【讨论】:

    • 我用过这个,唯一再次出现的就是提交按钮
    • 什么都没有,代码运行方式与。之前没有在模板中显示输入表单
    • @MattB,我已经在我的回答中更新了 add.html。
    【解决方案2】:

    无论是使用 Django 表单还是 bootstrap|html 表单,请不要尝试同时包含两者。 这里的问题是你不应该使用

    {{post.title}} -> {{post}} and remove {{post.textarea}}
    

    更多信息请参考here

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 2017-12-15
      • 2018-04-07
      • 2014-08-12
      • 2017-10-20
      • 2018-08-21
      • 2011-09-04
      • 2019-02-23
      相关资源
      最近更新 更多