Undefined HTML controls

26 views
Skip to first unread message

Telly George

unread,
Aug 4, 2020, 4:11:29 PM8/4/20
to Django users
I have this problem where I navigate from my home page (index.html)  to another page 
new_page.html) but the data input on new_page.html is not being read by Python. It seems as if at that point the HTML controls do not exist or are not being rendered and so I am unable to read the values in the text box and text area control.

Can someone help. Here are my source files

File name: layout.html
Comment: This is a template HTML page

{% load static %}

<!DOCTYPE html>

<html lang="en">
    <head>
        <title>{% block title %}{% endblock %}</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
        <link href="{% static 'encyclopedia/styles.css' %}" rel="stylesheet">
    </head>
    <body>
        <div class="row">
            <div class="sidebar col-lg-2 col-md-3">
                <h2>Wiki</h2>
                <form>
    {% csrf_token %}
                  </form>
                <div>
                    <a href="{% url 'index' %}">Home</a>
                </div>
                <div>                    
<a href="{% url 'new_page'%}">Create New Page</a>
                </div>
                <div>
                    Random Page
                </div>
                {% block nav %}
                {% endblock %}
            </div>
            <div class="main col-lg-10 col-md-9">
                {% block body %}
                {% endblock %}
            </div>
        </div>

    </body>
</html>

File name: index.html
Comment: default home page

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

{% block title %}
    Encyclopedia
{% endblock %}

{% block body %}

{% endblock %}

File name: new_page.html
Comments: This is the page with the controls where I cannot read the information typed by the user

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

{% block title %}
    Encyclopedia
{% endblock %}

{% block body %}
    <h1>New Wiki Entry</h1>
        <label>Entry Title: </label> <input type="text" name="title_box" id="title_box"><br>
        <label>Markdown Text for entry:</label><br> 
<textarea name="entry_details" id="entry_details" rows="3" cols="10"></textarea><br>
        <button type ="button" class="btn btn-primary btn-lg"formaction = "{% url 'add_entry' %}"> Add Entry</button>
<a href = "{% url 'add_entry' %}"> Add Entry </a>
{% endblock %}

File name: urls.py

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("new_page",views.new_page,name="new_page"),
    path("add_entry",views.add_entry,name="add_entry")     
]


File name: views.py

from django.shortcuts import render
from django.http import HttpResponse



def index(request):
    return render(request, "encyclopedia/index.html")

    
def new_page(request):
    return render(request,"encyclopedia/new_page.html")

    
def add_entry(request):
     #entry_title = request.POST.get('title_box')
     #entry_contents = request.POST.get('entry_details')
     #entry_title = request.GET["title_box"]
     #entry_contents = request.GET["entry_details"]
     entry_title = "Title"
     entry_contents = "Contents"
     
     if 'title_box' in request.GET:
              entry_title = request.GET.get('title_box')
              print("Found in GET...")
     if 'title_box' in request.POST:
              entry_title = request.POST.get('title_box')
              print("Found in POST...")
     
         
     print(f"entry_title: {entry_title}")
     print(f"entry_contents: {entry_contents}")
     
     
     return HttpResponse("Add Wiki Entry")

Ryan Nowakowski

unread,
Aug 11, 2020, 2:54:22 PM8/11/20
to Django users
On Tue, Aug 04, 2020 at 12:20:49PM -0700, Telly George wrote:
> File name: new_page.html
> Comments: This is the page with the controls where I cannot read the
> information typed by the user
>
> {% extends "encyclopedia/layout.html" %}
>
> {% block title %}
> Encyclopedia
> {% endblock %}
>
> {% block body %}
> <h1>New Wiki Entry</h1>
> <label>Entry Title: </label> <input type="text" name="title_box"
> id="title_box"><br>
> <label>Markdown Text for entry:</label><br>
> <textarea name="entry_details" id="entry_details" rows="3"
> cols="10"></textarea><br>
> <button type ="button" class="btn btn-primary btn-lg"formaction =
> "{% url 'add_entry' %}"> Add Entry</button>
> <a href = "{% url 'add_entry' %}"> Add Entry </a>
> {% endblock %}
>

Looks like you need to wrap your label, textarea and button fields in
<form></form>. Then you may need to change your button type to
"submit".
Reply all
Reply to author
Forward
0 new messages