Python Django:表单在网页上不可见

Python Django:表单在网页上不可见,python,django,django-forms,django-views,Python,Django,Django Forms,Django Views,我试图创建一个使用表单的简单Django网页,但我的表单不可见。我已经阅读了所有Django文档,并阅读了与此问题相关的多个问题,但我没有找到解决问题的方法 以下是相关文件: 视图.py from django.shortcuts import render from .forms import FileForm with open('calendar.txt') as f: file_content = f.read() def home(request): return rend

我试图创建一个使用表单的简单Django网页,但我的表单不可见。我已经阅读了所有Django文档,并阅读了与此问题相关的多个问题,但我没有找到解决问题的方法

以下是相关文件:

视图.py

from django.shortcuts import render
from .forms import FileForm

with open('calendar.txt') as f:
  file_content = f.read()

def home(request):
  return render(request, 'main/index.html',{'file_content':file_content})

def form_get(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 = FileForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
      pass
  else:
    form = FileForm()
  return render(request, 'index.html', {'form': FileForm.form})
from django.conf.urls import url
from django.contrib import admin
from main import views

urlpatterns = [
  url(r'^admin/', admin.site.urls),
  url(r'^$', views.home, name='home'),
]
{% extends "base.html" %}

{% block content %}
  <h1>Welcome to the calendar!</h1>
  <form action="/#" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
  </form>
  {{form}}
{% endblock content %}
url.py

from django.shortcuts import render
from .forms import FileForm

with open('calendar.txt') as f:
  file_content = f.read()

def home(request):
  return render(request, 'main/index.html',{'file_content':file_content})

def form_get(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 = FileForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
      pass
  else:
    form = FileForm()
  return render(request, 'index.html', {'form': FileForm.form})
from django.conf.urls import url
from django.contrib import admin
from main import views

urlpatterns = [
  url(r'^admin/', admin.site.urls),
  url(r'^$', views.home, name='home'),
]
{% extends "base.html" %}

{% block content %}
  <h1>Welcome to the calendar!</h1>
  <form action="/#" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
  </form>
  {{form}}
{% endblock content %}
index.py

from django.shortcuts import render
from .forms import FileForm

with open('calendar.txt') as f:
  file_content = f.read()

def home(request):
  return render(request, 'main/index.html',{'file_content':file_content})

def form_get(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 = FileForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
      pass
  else:
    form = FileForm()
  return render(request, 'index.html', {'form': FileForm.form})
from django.conf.urls import url
from django.contrib import admin
from main import views

urlpatterns = [
  url(r'^admin/', admin.site.urls),
  url(r'^$', views.home, name='home'),
]
{% extends "base.html" %}

{% block content %}
  <h1>Welcome to the calendar!</h1>
  <form action="/#" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
  </form>
  {{form}}
{% endblock content %}
{%extends“base.html”%}
{%block content%}
欢迎来到日历!
{%csrf_令牌%}
{{form}}
{{form}}
{%endblock内容%}

从我所读到的内容来看,我怀疑
url.py
文件中可能存在问题,但我已经查看了很多次,没有发现任何错误。有什么想法吗?

试试看

 def form_get(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 = FileForm(request.POST)
    # check whether it's valid:
    if form.is_valid():
      pass
  else:
    form = FileForm()
  return render(request, 'main/index.html', {'form': form})
查看我如何将渲染的上下文从
{'form':FileForm.form}
更改为
{'form':form}
。index.html文件的路径也错误

在修复视图之后,您需要添加一个实际的URL来访问它。您当前的URL已被删除

url(r'^$', views.index, name='home'),
注意如何使用
视图。索引
而不是
视图。表单获取
。将URL更改为使用
form\u get
,它就会工作

url(r'^$', views.form_get, name='home'),
不知道您是否希望
/
转到表单,或者您是否希望
/
仍然转到主页,在那里您有一个指向表单的链接。但在这种情况下,您不希望共享相同的
index.html
文件


但是看起来您可能正在尝试合并这两个视图,但是在这种情况下,您需要一个视图,它既可以显示文件的内容,也可以请求文件。但是,如果您有两个视图,让表单只接受输入,然后重定向到第二个视图以显示结果,则会更容易。

请参阅我的更正。我编辑了Repl.it,可以看到表单工作。但是您需要更好地了解您的URL(我假设正在工作)好的,现在我得到一个TemplateNotFoundError。我将
表单\u get
视图合并到
索引
视图中以加载这两个视图是否有意义?对于未找到的模板,请参阅我的上次编辑。您需要
呈现(请求,'main/index.html',{'form':form})
。我不知道您想做什么,因此我无法对合并两个视图发表评论,但只要您使用相同的模板,那么答案可能是肯定的