How to do mapping user input HTML page to view.py in django?

北慕城南 提交于 2020-05-17 09:04:09

问题


I am a newbie in Django and not able to map my user-input to my stored procedure query.

views.py

from django.db import connection
from django.shortcuts import render
from .forms import InputForm
def home_view(request):
    print("woooooooooooo",request)
    context1 ={}

    context1['form']= InputForm()
    print("context1::::", context1)
    return render(request, "input.html", context1)

def itemnumber(request):
    cursor = connection.cursor()
    try:
        itemnumber = "23241715"
        C=cursor.execute(f"EXEC ValidateBusinessrule '0000000000{itemnumber}'")
        print("c value",C)
        result_set = cursor.fetchall()
        print(result_set)
        result_set1= [' {} '.format(x) for x in result_set]
        print(result_set1)
        context = {"row": result_set1}
        print("lolllllllllll", context)
        return render(request, "home.html", context)
    finally:
        cursor.close()
        print("hello3")

forms.py

from django import forms
import re
# creating a form
class InputForm(forms.Form):
    print("inside forms")
    regex = re.compile('^([1-9]{8})$', re.UNICODE)
    ENTER_ITEM_NUMBER= forms.RegexField(max_length=8, regex=regex,help_text=("Required 8 digits 
    between {0-9}."))
    print("iiiiiiiiiiiiiiiiiiiiiiii",ENTER_ITEM_NUMBER)
    #ENTER_ITEM_NUMBER = forms.IntegerField(max_length=8, label='ENTER ITEM NUMBER')

item/urls

from django.urls import path
from django.urls import include, path
#from django.urls import path
from . import views

urlpatterns = [

    path('inputs/', views.home_view),
    path('item/', views.itemnumber, name='item'),
]
print("hello1")

input.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action = "" method = "post">
    {% csrf_token %}
    {{form }}
    <input type="submit" value=Submit">
</form>

</body>
</html>

Home.html

 <html>
        <style>
                body
                {
                background-color: powderblue;
                }
            </style>
     <body>
        <table style="border: 1px solid #b50b32; margin: 30px auto; width: 100px; padding: 0; border-spacing: 10px;" bgcolor="#00FF00" cellspacing="0" cellpadding="10">
            <tr>
                <ul>
                    <th>(u,v)</th>

                </ul>
            </tr>
            <tr>

                <ul >
                    {% for row in row %}
                    <td style= "text-align: center;">
                        {{  row }}
                    </td>
                </ul>
            </tr>
            {% endfor %}
        </table>
    </body>
 </html>

I am not using any model.*

(Can I have a Django form without Model)

I got confused How can I give my user input = ENTER_ITEM_NUMBER to the def itemnumber(request).

So that all things will work fine?


回答1:


Try something like that:

def itemnumber(request): 
    if request.method == 'POST': 
        form = InputForm(request.POST):
            if form.is_valid():
                # rest of your code. Now you should access form data, using form.PARAMETER_NAME

More info on topic you can find here

Also, you should define your form's action. You can do it like that:

<form action = "{% url 'app_name:item' %}" method = "post">

For more info on built-in tags refer to this page



来源:https://stackoverflow.com/questions/60925818/how-to-do-mapping-user-input-html-page-to-view-py-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!