输入没有以Django形式显示

2024-09-26 18:16:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我的表单输入没有显示。只是个按钮。我想主页.html你没拿到这张表格吗

你知道吗表单.py你知道吗

class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)

你知道吗视图.py你知道吗

def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
    # create a form instance and populate it with data from the request:
    form = NameForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
        # process the data in form.cleaned_data as required
        # ...
        # redirect to a new URL:
        return HttpResponseRedirect('/')

# if a GET (or any other method) we'll create a blank form
else:
    form = NameForm()

return render(request, 'mainApp/homepage.html', {'form': form})

你知道吗主页.html你知道吗

{% extends "mainApp/wrapper.html" %}
{% block title %}Главная{% endblock %}
{% block content %}
<h1>Main page</h1>

{% include "mainApp/includes/somehtml.html" %}
<br>

<form action="/account/username/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
<br>
{% endblock %}

它只使用“提交”按钮。我该怎么修?你知道吗


Tags: thenamepyform表单dataifrequest
2条回答

请换衣服

{{form}}

{{form.as_p}}

视图.py

from app_name.forms import * # Change app_name with your app name


def get_name(request):
  temp_context = {} # Check here

  if request.method == 'POST':
    acc_form = NameForm(request.POST) # Check here
    temp_context["acc_form"] = acc_form # Check here

    if acc_form.is_valid(): # Check here
      return HttpResponseRedirect('/')
  else:
      temp_context[“acc_form”] = NameForm() # Check here

  return render(request, 'mainApp/homepage.html', temp_context) # Check here

主页.html

{% extends "mainApp/wrapper.html" %}
{% block title %}Главная{% endblock %}
{% block content %}
  <h1>Main page</h1>

  {% include "mainApp/includes/somehtml.html" %}
  <br>

  <form action="/account/username/" method="post">
    {% csrf_token %}
    {{ acc_form }} # Check here; 
    # you can also try {{ acc_form.as_table }} or {{ acc_form.as_p }} if there any issue
    <input type="submit" value="Submit" />
  </form>
  <br>
{% endblock %}

相关问题 更多 >

    热门问题