Nishant Singh
Nishant Singh

Reputation: 511

how to send form data from one page to another using django

greetings/template/index.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
  <title> </title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" type="text/css" href="{% static 'greetings/css/style.css' %}" />
    {% verbatim %}
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    {% endverbatim %}
</head>
<body >
  <div class="container-fluid" >
    <div id="home" class="">
              <form class="input-form text-center" name = "form"  method = "POST"  action="{% url 'greetings.views.greetings'%}">
                  {% csrf_token %}
                <h1> Create your wishes </h1>
                  <input type="text" name = "username" placeholder="Enter your name"/>
                  <select name="select">
                      <option value=''>Please Choose your wish</option>
                  </select>
                  <br/>
                  <input id="btn" type="submit" name="submit" value="Create"/>
              </form>
    </div>
  </div>
</body>
</html>

greetings/template/greetings.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="demo-wish"class="card text-center">
              <h4> {{ username }} </h4>
           <h3> Wishes you </h3>
           <h4> Happy{{ username }} </h4>
          </div>
        <div id="demo" class="card text-center">
            <form>
                <h2>Create Your Wishes</h2>
                <h4>ENTER YOUR NAME TO WISH YOUR FRIENDS AND FAMILY MEMBERS</h4>
                <span>{{ username }}</span><br/>
                <input type="text" placeholder="Enter your name">
                <input type="submit" class="btn-primary"   value="Go">
            </form>
        </div>
      </div>
</body>
</html>

greetings/views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from greetings.forms import LoginForm


def index(request):
    template = loader.get_template('greetings/index.html')
    return HttpResponse(template.render(request))

def greetings(request):
    if request.method == "POST":
        # Get the posted form
        MyLoginForm = LoginForm(request.POST)

        if MyLoginForm.is_valid():
            username = MyLoginForm.cleaned_data['username']
    else:
        MyLoginForm = LoginForm()

    return render(request, 'greetings/greetings.html', {"username": username})

greetings/urls.py

from django.conf.urls import url,include
from django.contrib import admin
from . import views

urlpatterns = [
    url(r'^$', views.index),
    url(r'^admin/', admin.site.urls),
]

project/urls.py

from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r'^$', include('greetings.urls')),
    url(r'^admin/', admin.site.urls),
]

greetings/forms.py

from django import forms

class LoginForm(forms.Form):
   occasion=['christmas','New Year']
   user = forms.CharField(max_length = 100)
   select = forms.ChoiceField(widget=forms.Select(choices=occasion))

Error-Message:

NoReverseMatch at /
Reverse for 'greeting.views.greetings' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.10.4
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'greeting.views.greetings' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: C:\Users\Captain America\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-1.10.4-py3.6.egg\django\urls\resolvers.py in _reverse_with_prefix, line 392
Python Executable:  C:\Users\Captain America\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.0
Python Path:    
['D:\\django\\project_greetings',
 'C:\\Users\\Captain '
 'America\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
 'C:\\Users\\Captain '
 'America\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
 'C:\\Users\\Captain '
 'America\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
 'C:\\Users\\Captain America\\AppData\\Local\\Programs\\Python\\Python36-32',
 'C:\\Users\\Captain '
 'America\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages',
 'C:\\Users\\Captain '
 'America\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\django-1.10.4-py3.6.egg']
Server time:    Sat, 7 Jan 2017 17:58:39 +0530

Upvotes: 4

Views: 10710

Answers (2)

Ashish Singh
Ashish Singh

Reputation: 347

suppose you have two URLs, url1 for page 1 and url2 for page2 where you want your form data to be sent.

Template of page1 #on click Button we gonna direct to page2 i.e. url2

<form action="{% url 'url2' %}" method="post">
  {% csrf_token %}
    <button type="submit" name="var_name" value="var_value" class="class_name">Button</button>
</form>

views.py of page2

if 'var_name' in request.POST:
    #context is the name of dict that we gonna pass to our template
    #'var_name' derives from the template of page1
    context["name"] = request.POST['var_name']
return render(request, 'page2.html', context)

Template of page2 #thus we can finally get the value from page1 which we store in 'name' var

<p>{{name}}</p>

Upvotes: 0

e4c5
e4c5

Reputation: 53774

It's the following line that's producing the error

<form class="input-form text-center" name = "form"  method = "POST"  action="{% url 'greetings.views.greetings'%}">

But whe you look at your urls.py you don't have a view defined like that. So you need to define it first. And then you need to assign it a name if you want to get the link wtih the url tag

When you name your URL patterns, make sure you use names that are unlikely to clash with any other application’s choice of names. If you call your URL pattern comment, and another application does the same thing, there’s no guarantee which URL will be inserted into your template when you use this name.

In fact, I recommend that you read the whole page. in the link.

Upvotes: 2

Related Questions