Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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
Django forms.CharField(首字母=“)_Django_Django Forms - Fatal编程技术网

Django forms.CharField(首字母=“)

Django forms.CharField(首字母=“),django,django-forms,Django,Django Forms,我的任务是生成页面的预填充内容,如下所示: 我需要类似上面的东西,你能帮我在代码中实现吗 class EditPageForm(forms.Form): content = forms.CharField(widget=forms.Textarea) def editPage(request, title): content = util.get_entry(title) if request.method == "POST": f

我的任务是生成页面的预填充内容,如下所示:

我需要类似上面的东西,你能帮我在代码中实现吗

class EditPageForm(forms.Form):
    content = forms.CharField(widget=forms.Textarea)

def editPage(request, title):
    content = util.get_entry(title)
    if request.method == "POST":
        form = EditPageForm(request.POST)
        if form.is_valid():
            new_content = form.cleaned_data["content"]
            util.save_entry(title, new_content)
        return HttpResponseRedirect(reverse("encyclopedia:entry", kwargs={'title': title})) 
    EditPageForm.initial = content
    return render(request, "encyclopedia/editpage.html", {
        "form": NewSearchForm,
        "editPageForm": EditPageForm,
        "title": title
    })
Html代码:

<form action="{% url 'encyclopedia:editpage' title=title%}" method="post">
    <div>
        <h3>Content:</h3>
        {% csrf_token %}
        {{ editPageForm }}
    </div>
    <input type="Submit">
    </form>

内容:
{%csrf_令牌%}
{{editPageForm}}
您可以将带有初始值的字典作为值传递给表单:

from django.shortcuts import redirect

def editPage(request, title):
    content = util.get_entry(title)
    if request.method == 'POST':
        editPageForm = EditPageForm(request.POST, initial={'content': content})
        if form.is_valid():
            new_content = form.cleaned_data['content']
            util.save_entry(title, new_content)
        return redirect('encyclopedia:entry', title=title)
    else:
        editPageForm = EditPageForm(, initial={'content': content})
    return render(request, 'encyclopedia/editpage.html', {
        'form': NewSearchForm,
        'editPageForm': editPageForm,
        'title': title
    })
从django.shortcuts导入重定向
def编辑页(请求,标题):
content=util.get\u条目(标题)
如果request.method==“POST”:
editPageForm=editPageForm(request.POST,initial={'content':content})
如果form.is_有效():
新建内容=表单。已清理的内容数据['content']
util.save_条目(标题、新内容)
返回重定向('encyclopedia:entry',title=title)
其他:
editPageForm=editPageForm(,initial={'content':content})
返回呈现(请求'encyclopedia/editpage.html'{
“表单”:新闻搜索表单,
“editPageForm”:editPageForm,
“标题”:标题
})
也就是说,使用模型和a可能更好,因为这样可以删除大量样板代码。

您可以将带有初始值的字典作为值传递给表单:

from django.shortcuts import redirect

def editPage(request, title):
    content = util.get_entry(title)
    if request.method == 'POST':
        editPageForm = EditPageForm(request.POST, initial={'content': content})
        if form.is_valid():
            new_content = form.cleaned_data['content']
            util.save_entry(title, new_content)
        return redirect('encyclopedia:entry', title=title)
    else:
        editPageForm = EditPageForm(, initial={'content': content})
    return render(request, 'encyclopedia/editpage.html', {
        'form': NewSearchForm,
        'editPageForm': editPageForm,
        'title': title
    })
从django.shortcuts导入重定向
def编辑页(请求,标题):
content=util.get\u条目(标题)
如果request.method==“POST”:
editPageForm=editPageForm(request.POST,initial={'content':content})
如果form.is_有效():
新建内容=表单。已清理的内容数据['content']
util.save_条目(标题、新内容)
返回重定向('encyclopedia:entry',title=title)
其他:
editPageForm=editPageForm(,initial={'content':content})
返回呈现(请求'encyclopedia/editpage.html'{
“表单”:新闻搜索表单,
“editPageForm”:editPageForm,
“标题”:标题
})

也就是说,使用模型和a可能更好,因为这样可以删除大量样板代码。

您可以使用小部件属性在表单中添加您想要的占位符文本,该文本可以是动态的,并且添加的行数更少

class EditPageForm(forms.Form):
    content = forms.CharField(widget=forms.Textarea(attrs={'placeholder': 'THIS IS MY PLACEHOLDER TEXT'}))

您可以使用小部件属性在表单中添加您想要的占位符文本,该文本可以是动态的,并且可以添加更少的行

class EditPageForm(forms.Form):
    content = forms.CharField(widget=forms.Textarea(attrs={'placeholder': 'THIS IS MY PLACEHOLDER TEXT'}))

感谢大家的帮助,找到了解决方案

return render(request, "encyclopedia/editpage.html", {
        "form": NewSearchForm,
        "editPageForm": EditPageForm(initial={'content': content}),
        "title": title
    })

现在看来,

谢谢大家的帮助,解决方案已经找到了

return render(request, "encyclopedia/editpage.html", {
        "form": NewSearchForm,
        "editPageForm": EditPageForm(initial={'content': content}),
        "title": title
    })

现在看起来是这样的,

您可以指定您的代码不起作用的原因吗?请包含有关您为尝试确定问题所在而进行的故障排除的信息。请指定代码不起作用的原因?请包括有关您为尝试并确定问题所在而进行的故障排除的信息。