moltarze
moltarze

Reputation: 1501

Django: forms not visible on the webpage

I am trying to create a simple Django webpage that uses forms, but my forms are not visible. I have read all of the Django docs and read multiple questions related to this issue, but I have found no solution that fixes my problem.

Here are the relevant files:

views.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})

urls.py

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'),
]

index.py

{% 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 %}

Link to program

From what I have read, I suspect there may be an issue in the urls.py file, but I've been looking over it many times and I haven't found anything wrong. Any thoughts?

Upvotes: 0

Views: 557

Answers (1)

dkarchmer
dkarchmer

Reputation: 5594

Try

 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})

See how I changed the context for the render from {'form': FileForm.form} to {'form': form}. The path to the index.html file was also wrong.

After fixing the view, you need to add an actual URL to go to it. Your current URL has

url(r'^$', views.index, name='home'),

Note how is using views.index and not views.form_get. Change the URL to use form_get and it will work.

url(r'^$', views.form_get, name='home'),

Don't know if you want to have / go to the form, or if you would rather have / still go to home, where you have a link to the form. But in that case, you do not want to share the same index.html file.

But seems like you may be trying to merge those two, but in that case, you need a single view, which can both show the content of the file, and ask for the file. But will be easier if you have two views, and leave the form to just take the input, and then redirect to the second view to show the results.

Upvotes: 1

Related Questions