简体   繁体   English

传一个变量给另一个function,同时在django渲染一个html模板

[英]Pass a variable to another function and render an html template at the same time in django

We want to access the same variable in every function inside our views.py.我们想在 views.py 中访问每个 function 中的同一个变量。 Since it is not constant, we cannot use it as a global variable.由于它不是常量,我们不能将它用作全局变量。

Is it possible to pass a variable to another function while also rendering an HTML template?是否可以将变量传递给另一个 function 同时渲染 HTML 模板? What are the alternatives if none exist?如果没有替代方案,有哪些替代方案?

This is our login function in views.py这是我们在 views.py 中的登录 function

def loginpage(request):
    errorMessage = ''
   
    # Applicant Login
    if request.method=="POST": 
        if request.POST.get('username') and request.POST.get('pwd'):
            try:
                currentUser=Applicant.objects.get(username=request.POST['username'],pwd=request.POST['pwd'])
                currentUser=Applicant.objects.get(username=request.POST['username'])
                first = currentUser.firstname
                middle = currentUser.middleinitial
                last = currentUser.lastname
                AppDashboard = ApplicantDashboardPageView(currentUser, request)

            except Applicant.DoesNotExist as e:
                errorMessage = 'Invalid username/password!'

return render(request, 'home.html')

The currentUser variable inside our login function is the variable we want to pass in this function我们登录function里面的currentUser变量就是我们要在这个function中传递的变量

def ApplicantdashboardPageView(currentUser, request):
    appPeriod = ApplicationPeriod.objects.all()
    exam = ExaminationSchedule.objects.all()
    posts = Post.objects.all().order_by('-created_on')
    form = PostForm()
    name=userNaCurrent
    print('from storeCurrentUser', name)

    if request.method == "GET":
        try:
            posts = Post.objects.all().order_by('-created_on')
            form = PostForm()
            #applicantID=currentUser.id  
            #applicantNotification = Applicant.objects.get(id=applicantID)
            return render(request, 'applicantdashboard.html', context={'UserName' : name,  'posts':posts,  'appPeriod':appPeriod, 'exam':exam})
        except ObjectDoesNotExist:
            return render(request, 'applicantdashboard.html', context={'UserName' : name,  'posts':posts,})
        
    return render(request, 'applicantdashboard.html', context={'UserName' : name,  'posts':posts,  'appPeriod':appPeriod, 'exam':exam})

I am new to Django so please bear with me if my question seem too basic.我是 Django 的新手,所以如果我的问题看起来太基本,请耐心等待。 Thank you谢谢

Store raw user password is a very big flaw in security.存储原始用户密码是一个非常大的安全漏洞。 Please read more about Django Authentication system https://docs.djangoproject.com/en/4.1/topics/auth/请阅读更多关于 Django 认证系统https://docs.djangoproject.com/en/4.1/topics/auth/

Basically, to store critical confidential information like passwords you need to at least, encrypt it.基本上,要存储密码等关键机密信息,您至少需要对其进行加密。 But for passwords you don't need to see the raw value of it, isn't it?但是对于密码,您不需要查看它的原始值,不是吗? Therefore, you just need to hash it and compare it every time you need to authenticate the user.因此,你只需要 hash 它并在每次需要对用户进行身份验证时进行比较。 Read more here Best way to store password in database在这里阅读更多在数据库中存储密码的最佳方式

Django Auth system will also help to solve the issue by injecting the current user into a "global" request object so that you can access it everywhere. Django 身份验证系统也将通过将当前用户注入“全局”请求 object 来帮助解决问题,以便您可以在任何地方访问它。

You can do the same by keeping those 2 methods in a class and accessing variables by creating objects for it.您可以通过将这 2 个方法保留在 class 中并通过为其创建对象来访问变量来执行相同的操作。

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

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