1

I am having an issue when it cmoes to passing variables from templates to views. Even though I am able to pass variables from view to template, I canot seem to get it right. I have looked at similar questions here.

Following the Django docs I created a forms.py script as follows:

forms.py

GNU nano 2.7.4 File: forms.py

from django import forms

class TactForm(forms.Form):
        tacttime = forms.CharField(label='Tact Time', max_length=100)

Updated View

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

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

from lineoee.models import Lineoee31

from .forms import TactForm

def details(request):
    if request.method == 'POST':
            form = TactForm(request.POST)
            print(form)
    else:
            form = TactForm()

    context = {'form' : form}

    return render(request, 'linedetails/index.html',context)

Updated Template

            <form method="POST" action="{% url 'details' %}">
                    {% csrf_token  %}
                    {{ form.as_p }}
                    <label for="tacttime">Tact Time: </label>
                    <input id="tacttime" type="text" name="tacttime" value ="60">
                    <input type="submit" value="OK">
            <form>

Updated URLS

from django.conf.urls import url
from django.contrib import admin
from lineoee.views import index
from lineoee.views import details

urlpatterns = [
    url(r'lineoee/$', index, name='index'),
    url(r'linedetails/', details, name='details'),
]

Still, no errors and no values passed to the view.


EDIT

I am now getting some data on pressing the OK button, however it is not what I was expecting. I want to be able to retrieve the text entered into the input field. How can I do this?

"POST /linedetails/ HTTP/1.1" 200 24580

<tr><th><label for="id_tacttime">Tact Time:</label></th><td><input 
type="text" name="tacttime" value="60" required id="id_tacttime" 
maxlength="100" /></td></tr>
12
  • any error you are getting? most probably you will get the csrf validation error.
    – aman kumar
    Commented Aug 22, 2018 at 9:18
  • No errors whatsoever. Page is rendered accordingly but nothing is printed to console when pressing submit.
    – fcp
    Commented Aug 22, 2018 at 9:19
  • can you past the console log after post?
    – aman kumar
    Commented Aug 22, 2018 at 9:20
  • Can you share your URL that's pointing to that view? Commented Aug 22, 2018 at 9:20
  • 1
    You're posting to the wrong URL. <form method="POST" action="/details"> should be <form method="POST" action="/linedetails"> Commented Aug 22, 2018 at 9:25

2 Answers 2

1

Template

<div style="text-align:center;">
    <form method="POST" action="{% url 'details' %}">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="adsfadsfas">
    </form>
</div>

Views (EDITED)

Whatever the name you use in your input on your HTML template, that's the key you're gonna use to get what comes in the request.POST. That's why you'd like to use {{ form.field }} in the template so you know beforehand the name of the fields you're expecting to come in the request.POST

def details(request):
    if request.method == 'POST':
            print(request.POST)
            print(request.POST.get('tacttime')
            form = TactForm(request.POST)
            print(form)
    else:
            form = TactForm()

    return render(request, 'linedetails/index.html', context)

URLS

from django.conf.urls import url
from django.contrib import admin
from lineoee.views import index
from lineoee.views import details

urlpatterns = [
    url(r'lineoee/$', index, name='index'),
    url(r'linedetails/', details, name='details'),
]
9
  • Thanks, am in the process of trying this out. Agree with the views and urls, but don't understand the template part. What is form.as_p? Thanks
    – fcp
    Commented Aug 22, 2018 at 10:47
  • 1
    as_p is a method inside the form that renders the form as a p HTML tag. You can check it here Commented Aug 22, 2018 at 10:50
  • I believe I updated everything as you suggest, but to no avail. No errors but nothing prints to the console.
    – fcp
    Commented Aug 22, 2018 at 11:00
  • Appreciate your help, but nothing happens too. Following the docs I think I have some confusion with the templates. In the tuturial it shows two separate templates. One with the <input type="submit" value="OK"> line and another template with the <input type="submit" value="Submit"> line
    – fcp
    Commented Aug 22, 2018 at 12:05
  • That's weird, I mean, you not get anything on your post. I, with this code, could get the post in and print the form. Have you tried to stop and start your server again? It sounds silly but who knows Commented Aug 22, 2018 at 12:11
-1
def details(request):
    if request.method == 'POST':
            var = request.POST['textfield']
            print(var)
1
  • Tried this, nothing happens.
    – fcp
    Commented Aug 22, 2018 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.