到目前为止我的代码是:
<div class="icon-and-title-flex">
<img src="static/img/profile.png" class="appicon"></img>
<div class="title-container">
<!-- REPLACE WITH YOUR APP NAME -->
<span class="text-title">Dashboard</span>
<br><div class="intertext-padding"></div>
<!-- REPLACE WITH YOUR DEV NAME -->
<span class="text-subtitle">by Student Accounts</span>
<br><div class="intertext-padding"></div>
<!-- REPLACE WITH YOUR APP PRICE -->
<span class="text-subtitle">{{ user.first_name }}</span>
</div>
我有{{user.first_name}},我想从models.py调用一个变量并在那里显示它。这是什么正确的语法? (这似乎不起作用。)
编辑:
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.conf.urls import url
from .models import UserInfo, Events
from django import forms
from .forms import RegisterForm, LoginForm, OrderForm
from django.contrib.auth.models import User
from django.shortcuts import redirect
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import authenticate, login, logout
# Create your views here.
def home(request):
return render(request, 'student/index.html')
@csrf_exempt
def signin(request):
print "login"
if request.method == 'POST':
form = LoginForm(request.POST)
username = request.POST['username']
password = request.POST['password']
print "input username ", username
try:
if form.is_valid():
user = authenticate(username=username, password=password)
if user is not None:
print "user not none"
print user.username
print user.email
login(request,user)
return redirect("/")
else:
print "login failed"
raise forms.ValidationError({'username':['Invalid username/password']})
else:
print form.errors
except:
raise
else:
print "hello"
form = LoginForm()
return render(request, 'student/login.html', {'form': form})
@csrf_exempt
def signup(request):
print "signup"
if request.method == 'POST':
print "post signup"
form = RegisterForm(request.POST)
try:
if form.is_valid():
print form.cleaned_data
u = User.objects.create_user(form.cleaned_data['emailid'], form.cleaned_data['emailid'], form.cleaned_data['passwd1'] )
ui = UserInfo()
ui.user = u
ui.class_of = form.cleaned_data['gradyear']
ui.grade = form.cleaned_data['grade']
ui.balance = 0
ui.save()
user = authenticate(username=form.cleaned_data['emailid'], password=form.cleaned_data['passwd1'])
login(request,user)
print "after login in signup"
return redirect("/")
else:
print "error"
print form.errors
except:
raise
print "error here"
print form.errors
pass
#return render(request, 'student/register.html', {'form': form})
else:
form = RegisterForm()
return render(request, 'student/register.html', {'form': form})
def forgotpassword(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
print form.cleaned_data
return HttpResponseRedirect('/thanks/')
else:
print "INVALID"
print form.errors
else:
form = LoginForm()
return render(request, 'student/forgotpassword.html')
def studentinfo(request):
return render(request, 'student/studentinfo.html', {} )
def error(request):
return render(request, 'student/LoginError.html', {} )
def site_logout(request):
logout(request)
return redirect("/")
#return render(request, 'student/studentinfo.html', {} )
def order(request):
if request.method == 'POST':
form = 0
return render(request, 'student/orderform.html')
def dashboard(request):
return render(request, 'student/dashboard.html')
这是我的view.py,我不知道问题所在。
答案 0 :(得分:0)
您的模板正在使用+----+----+----+
| | | |
| | | |
+----+----+----+
| | | |
| | | |
+----+----+----+
| | | |
| | | |
+----+----+----+
,但您在views.py中的{{ user.first_name }}
方法未将任何dashboard
对象传递给模板,因此您的模板不会显示任何用户。听起来你想获得当前用户的信息,所以你的观点应该是:
user
您需要了解为什么需要从view方法传递变量才能使用它,doc就在这里。