简体   繁体   English

模板数据未传递到 Django view.py

[英]Template data not passing into Django view.py

在此处输入图像描述

We are trying to get the name, email-ID and password through post method in the form, we get the data as none instead of the data filled in the form.我们试图通过表单中的 post 方法获取姓名、电子邮件 ID 和密码,我们将数据设置为 none,而不是表单中填写的数据。

view.py视图.py

from django.http import HttpResponse 
# Create your views here.

def sign_up_in(response):
    email = response.POST.get('eemail')  #Getting the data of email by the name of eemail
    print(email)                         #Trying to print the email that we got from the form
    return render(response, "main/sign_up_in.html",{}) 

HTML code HTML代码

There are two forms and we are trying to get the data from the first form.有两个 forms 我们正在尝试从第一个表单中获取数据。 The first form is for creating the account and the second is for signing in.第一个表单用于创建帐户,第二个表单用于登录。

<form method="POST" class="form" id="a-form" action="">
                    <h2 class="form_title title">Create Account</h2>
                    <div class="form__icons"><img class="form__icon" src="svg_data" alt=""><img class="form__icon" src="svg_data"><img class="form__icon" src="svg_data"></div><span class="form__span">or use email for registration</span>
                    <input class="form__input" type="text" placeholder="Name" name="eemail">
                    <input class="form__input" type="text" placeholder="Email">
                    <input class="form__input" type="password" placeholder="Password">
                    <button class="form__button button submit">SIGN UP</button>
                </form>
<form method="POST" class="form" id="b-form" action="">
                    <h2 class="form_title title">Sign in to Website</h2>
                    <div class="form__icons"><img class="form__icon" src="svg_data" alt=""><img class="form__icon" src="svg_data"><img class="form__icon" src="svg_data"></div><span class="form__span">or use your email account</span>
                    <input class="form__input" type="text" placeholder="Email" name="eemail">
                    <input class="form__input" type="password" placeholder="Password"><a class="form__link">Forgot your password?</a>
                    <button class="form__button button submit">SIGN IN</button>
                </form>

url.py url.py


from . import views
urlpatterns = [
   path("", views.Home, name = "Homes"), 
   path("sign/", views.sign_up_in, name = "sign_up_in") 
]

**You should do it like this ** Write request instead of response and check that the method is GET or POST **你应该这样做**写请求而不是响应并检查方法是GET还是POST

def sign_up_in(request):
    if request.method == 'POST':
        email = request.POST.get('eemail') 
        print(email)                         
    return render(request, "main/sign_up_in.html",{}) 

Inside HTML Add action where you want to post data using form (use name of the path) ex: for signup在 HTML 内部使用表单(使用路径名称)添加要发布数据的操作,例如:注册

<form method="POST" class="form" id="a-form" action="{% url 'sign_up_in' %}"> #path name
                <h2 class="form_title title">Create Account</h2>
                <div class="form__icons"><img class="form__icon" src="svg_data" alt=""><img class="form__icon" src="svg_data"><img class="form__icon" src="svg_data"></div><span class="form__span">or use email for registration</span>
                <input class="form__input" type="text" placeholder="Email" name="eemail">
                <input class="form__input" type="password" placeholder="Password">
                <button class="form__button button submit">SIGN UP</button>
            </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM