2016-09-30 69 views
0

我正在使用django reactjs,現在我已經維護會話,並希望通過reactjs訪問會話數據cutomise UI。 我能爲此做些什麼?如何reactjs訪問會話數據(用戶名)從django

//my models.py snippet 

class Users(models.Model): 
    gender = models.CharField(max_length=200) 
    username = models.CharField(max_length=50) 
    password = models.CharField(max_length=50) 
    first_name = models.CharField(max_length=30) 
    last_name = models.CharField(max_length=40) 
    email = models.EmailField() 
    address = models.CharField(max_length=50) 
    city = models.CharField(max_length=60) 
    state = models.CharField(max_length=30) 
    country = models.CharField(max_length=50) 
    bg = models.CharField(max_length=200) 
    badges = models.BigIntegerField(null=True) 
    dob = models.DateField() 
    contact = models.BigIntegerField(null=True) 
    age = models.BigIntegerField(default=0) 
    status = models.BigIntegerField(null=True) 




//my views.py snippet 

def login(request): 
    print(request.POST.get('username')) 
    username=str(request.POST.get('username')) 
    password=str(request.POST.get('password')) 

    if(Users.objects.filter(password=password, username=username).exists()): 
     request.session['username'] = username 
     name=request.session['username'] 
     return render(request, 'loggedin.html', {"username" : name}) 
    else: 
     return HttpResponse("incorrect data") 
+0

只是一個建議..不要混合反應和Django的前端開發..使用反應/ redux在前端,也許django在後端休息。在處理涉及這種情況的項目時,我遇到了很多麻煩,並且最終不得不在前端使用完全反應。 –

+1

您可以使用Django Rest Framework創建一個響應用戶信息的端點 - 端點接收正常的Request對象,因此您可以返回request.user.username等。 –

回答

2

要做到這一點的一種方法是遵循GitHub上的Scott Woodall的django-react-template
基本上,他的做法這是東西用戶請求數據到Django的「base.html文件」(該項目也被稱爲app.html),這是由後端(Django的)

送達前端方面,他通過'窗口'對象訪問數據。

{% load static %} 
<!-- This is basically the ONLY webpage created in Django 
see the window.django = {} part where variables are handed over initially to React 
top most the Django CSRF Token 
is this secure in Production? 
... 
... 
--> 

<!doctype html> 
<html> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <title>Django & React Starter</title> 
     ... 
     ... 
    </head> 
    <body> 
     <noscript>This application requires javascript to function.</noscript> 
     <div id="root"></div> 
     <script src="{% static "js/vendor.bundle.js" %}"></script> 
     <script> 
     window.django = { 
      csrf: "{{ csrf_token }}", 
      urls: { 
       logout: "{% url "logout" %}", 
       staticRoot: "{% static "" %}", 
       users: "{% url "emailuser-list" %}" 
      }, 
      user: { 
       username: "{{ request.user.username }}", 
       full_name: "{{ request.user.get_full_name }}", 
       last_login: "{{ request.user.last_login }}", 
       permissions: immutable.Set(
        {% autoescape off %}JSON.parse('{{ permissions }}'){% endautoescape %} 
       ) 
      } 
     }; 
     </script> 
     <script src="{% static "js/bundle.js" %}"></script> 
    </body> 
</html> 

所有學分斯科特·伍德爾O/我希望得到你開始!

+0

這就是我們在應用程序中執行的操作和它工作得很好。最初,當我們的js應用程序初始化時,我們曾考慮過爲服務器調用一大塊JSON,但是這太過複雜了。這種方法很簡單,而且完美。 –