简体   繁体   English

Django表单不会显示在模板上

[英]Django form won't show up on template

I'm making a sign in system in Django Python, i've been preparing the forms.py, views.py and the template itself, but so far the form fail to load on the template, can anyone help? 我正在Django Python中建立登录系统,我一直在准备Forms.py,views.py和模板本身,但是到目前为止,表单无法加载到模板上,有人可以帮忙吗?

Forms.py Forms.py

class Email_Only_SignUp_Form(forms.Form):
    email = forms.EmailField(initial='Your Email...')

Views.py Views.py

def email_only_signup_form(request, template_name):
    if request.method == 'POST':
        signup_form = Email_Only_SignUp_Form(request.POST)

        if signup_form.is_valid():
            email = signup_form.cleaned_data['email']
            username = email

            try:
                #check for duplicate username
                    User.objects.get(username=username)
                    email_error = 'email already exist'
            except:
                #initial creation of user object
                try:
                    import os, random, string

                    length = 13
                    chars = string.ascii_letters + string.digits + '!@$()'
                    random.seed = (os.urandom(1024))
                    password = ''.join(random.choice(chars) for i in range(length))

                    User.objects.create_user(username,
                                             username,
                                             password,
                    )
                    user = User.objects.get(username=username)
                    user_profile=UserProfile(user=user)
                    user_profile.save()

                    #send email to user
                    try:
                        admin_email = settings.EMAIL_ORIGIN_MEMBERS
                        email_txt = loader.get_template('account/emails/createaccount.html')
                        email_html = loader.get_template('account/emails/createaccounthtml.html')
                        email_context = Context({'u_name': username,
                                                 'username': username,
                                                 'password': password,
                                                })
                        new_user_mail = EmailMultiAlternatives('Welcome!',
                                                               email_txt.render(email_context),
                                                               admin_email,
                                                               [user.email, ],
                                                               headers={'Reply-To': 'admin@admin.com'}
                        )
                        new_user_mail.attach_alternative(email_html.render(email_context), 'text/html')
                        new_user_mail.send()
                    except:
                        pass

                    return redirect('/account/thankyou/?next=%s'%next)

                except:
                     pass

        else:
            print('user form in not valid')

    else:
        signup_form = Email_Only_SignUp_Form()

    return render_to_response(template_name, locals(), context_instance=RequestContext(request))

email_only_signup_form.html email_only_signup_form.html

{% extends "index.html" %}

{% block heroslider %}
    <div class="page_title2" style="padding:150px 0px 50px 0px;">
        <div class="container">

            <h1>User Registration</h1>

        </div>
    </div><!-- end page title -->
{% endblock %}

{% block main_body %}

<style type="text/css">
    input[type='radio'], input[type='checkbox'] {
        width:20px;
        vertical-align: middle;
    }

    div.reg_error {
        position:relative;
        top:-10px;
        margin-top:0px;
        padding-top:0px;
        color:red;
    }
</style>

<div class="container">
    <form class="pagesignup logiform" action="" method="POST">{% csrf_token %}
        <div class="row">
            <div class="large-12 columns" style="margin-bottom: 30px;">
                <div class="reg_form">
                    <div class="sky-form">
                        <header>REGISTER</header>
                    </div>

                    <div class="row">
                        <div class="large-12 columns">

                            <p>Email<br/>
                                {{signup_form.email}}
                            <div class="reg_error">{{ signup_form.email.errors.as_text }}{{email_error}}</div></p>

                        </div>
                    </div>
                    <div class="row">
                        <div class="large-12 large-centered columns" style="text-align:center;padding:20px;">
                            <input class="but_medium1" style="border:none;" type = "submit" value="REGISTER" /><br>
                            <br>By clicking on register, you have read and agreed to our <a href="/terms-of-use/">terms of use</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<!-- Google Code for Sign Up Page (landed) Conversion Page -->
<script type="text/javascript">
    /* <![CDATA[ */
    var google_conversion_id = 969557266;
    var google_conversion_language = "en";
    var google_conversion_format = "3";
    var google_conversion_color = "ffffff";
    var google_conversion_label = "5zU4CJby_FoQkoqpzgM";
    var google_remarketing_only = false;
    /* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
    <div style="display:inline;">
    <img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/969557266/?label=5zU4CJby_FoQkoqpzgM&amp;guid=ON&amp;script=0"/>
    </div>
</noscript>
{% endblock %}

You have not passed the signup_form to the template. 您尚未将signup_form传递给模板。

return render_to_response(template_name,  {'signup_form': signup_form}, context_instance=RequestContext(request))

I have no idea what locals() does. 我不知道locals()做什么。

Edit: I just saw locals which is a built in python function. 编辑:我刚刚看到了本地人,这是内置的python函数。 It will be better if you explicitly pass the variables you need in the template. 如果您在模板中显式传递所需的变量,将会更好。

Edit 2: Check if it is the correct template_name. 编辑2:检查它是否正确的template_name。 In the template simply print and see the form {{ signup_form }}. 在模板中,只需打印并查看表单{{signup_form}}。 See if it is available. 查看是否可用。

You are not returning the form. 您没有返回表格。 Try changing the last line of the view to 尝试将视图的最后一行更改为

return render_to_response(template_name, locals(), context_instance=RequestContext(request, {'signup_form' : signup_form ))

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

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