简体   繁体   English

在 Javascript 中获取 Django 视图字典键,但不是将其视为字符串,而是将其视为变量名称

[英]Getting Django view dictionary key in Javascript, but instead of considering it a String, considers a Variable Name

I am doing a project in Django. In my views I send data to the HTML pages with dictionaries.我在Django做一个项目。在我看来,我用字典将数据发送到HTML页面。 I can easily access that data on HTML, but when I try to access it on Javascript I can't because Javascripts thinks it's a variable name.我可以轻松访问 HTML 上的数据,但是当我尝试在 Javascript 上访问它时,我不能,因为 Javascripts 认为它是一个变量名。

For example, I have a log in page.例如,我有一个登录页面。 If the credentials are wrong, I send them to the same page and tell them that the username or password are wrong.如果凭据错误,我会将它们发送到同一页面并告诉他们用户名或密码错误。 I want to keep the previously written username on the form, so the person only needs to rewrite the password.我想保留之前在表单上写的用户名,所以这个人只需要重写密码即可。 I send the username from the view to the Javascript, but I can't access it.我将用户名从视图发送到 Javascript,但我无法访问它。

My view code:我的视图代码:

def login_form(request):
    assert isinstance(request, HttpRequest)
    if 'username' and 'password' in request.POST:
        user = request.POST['username']
        passw = request.POST['password']

        if rightcredentials(user,passw):
            tparams = {
                'message': 'login successful',
            }
            return render(request, 'about.html', tparams)
        else:
            tparams = {
                'login' : 'failure1',
                'username_info' : user
            }
            return render(request, 'login_form.html', tparams)

Javascript code: Javascript 代码:

{% if login == 'failure1' %}

    <form action="." method="post">
        {% csrf_token %}
            <div class="program-info">
                <h3 class="program-title">Sign Into Your Account</h3>
                <p>Wrong Username or password</p>
                <div class="form-group">
                    <input type="text" value="" class="form-control" name="username" id="username" placeholder="Username">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" name="password" placeholder="Password">
                </div>
                <button type="submit" class="btn btn-primary">Log In</button>
            </div>
    </form>
    <script type="text/javascript">
        {% autoescape off %}
        var paramvalue = {{ username_info }}
        {% endautoescape %}

        document.getElementById("username").value = paramvalue; 
    </script> 

{%endif%}

After inserting username: 'pedro' and the wrong password on the form I get the following error:在表单中插入用户名:'pedro' 和错误的密码后,出现以下错误:

Uncaught ReferenceError: pedro is not defined

Is there a way to access the string username_info or a way to convert the variable name to a string?有没有办法访问字符串username_info或将变量名转换为字符串的方法?

Wrapping the {{ }} in quotation marks should do the trick{{ }}括在引号中应该可以解决问题

{% autoescape off %}
var paramvalue = "{{ username_info }}"
{% endautoescape %}

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

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