Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

python - 努力将request.user.id传递给python / django中的SQL函数(Struggling to pass request.user.id to SQL function in python/django)

hoping someone can help as I'm new django (and coding) and I'm stuck with trying to pass the current user's ID number to an SQL function.

(希望有人能帮上忙,因为我是新的Django(和编码),但我一直试图将当前用户的ID号传递给SQL函数。)

I've spent days searching for a solution but nothing I've tried has worked.

(我花了几天时间寻找解决方案,但是我尝试过的任何方法都没有奏效。)

I know the SQL function works as I can pass through a number without any issue, but when trying to use request.user it just comes up blank.

(我知道SQL函数可以正常工作,因为我可以毫无问题地传递数字,但是当尝试使用request.user时,它只是空白。)

views.py:

(views.py:)

def my_custom_sql(request):
    current_user = User.objects.get(request.user)
    with connection.cursor() as cursor:
        cursor.execute("SELECT first_name FROM customuser WHERE id = %s",[current_user])
        row = cursor.fetchone()

    return row

def dashboard(request):
    return render(request, 'dashboard.html', {'my_custom_sql': my_custom_sql})

models.py:

(models.py:)

class CustomUser(AbstractUser):
    pass
    # add additional fields in here
    employee_dob = models.DateField(blank=True, null=True)
    is_executive = models.IntegerField(blank=True, null=True)

    def __str__(self):
        return self.username

dashboard.html:

(dashboard.html:)

<a href="{% url 'home' %}">Home</a> <br>

{% if user.is_authenticated %}

<p>Is this the your first name?: {{my_custom_sql}}</p>

{% else %}
    <p>You are not logged in, please do so.</p>

{% endif %}

I've read everything I can find around the issue but haven't found anything yet that has helped.

(我已经阅读了有关该问题的所有内容,但还没有发现有任何帮助。)

I'm not sure if it has something to do with sessions or django hashing the info?

(我不确定是否与会话或Django哈希信息有关?)

But I'm not sure how to get around this safely.

(但是我不确定如何安全地解决这个问题。)

Anyone got any idea how to resolve this so that the logged in user's id number (pk) is passed to current_user for this to work?

(任何人都知道如何解决此问题,以便将登录用户的ID号(pk)传递给current_user才能起作用?)

There's no error code for me to work around, it just returns a blank output where the user's first name should appear.

(没有可供我解决的错误代码,它只返回空白输出,应在其中显示用户的名字。)

Really trying to learn here so explanations of where I've gone wrong would be much appreciated.

(真正尝试在这里学习,对我犯错的地方的解释将不胜感激。)

  ask by K-15 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
# Just need to change the value of 'current_user' variable

def my_custom_sql(request):
    current_user = request.user.id
    with connection.cursor() as cursor:
        cursor.execute("SELECT first_name FROM customuser WHERE id = %s",[current_user])
        row = cursor.fetchone()

    return row

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...