我有一个代码,每当我使用这个代码时,它会自动创建会话值,比如打印{{user.username}}但是现在我做了一些更改而且代码不起作用,它没有打印值{{user.username}}。
views.py:_
import os ,sys
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.template.response import TemplateResponse
from django import forms
from django.contrib.auth import authenticate, login, logout
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.core.context_processors import csrf
from userprofile.forms import FuserRegistration,userLoginform
from django.views.decorators.csrf import csrf_exempt
from .forms import UploadFileForm
from django.conf import settings
#Register function:
def userRegistration(request,template=None,context=None):
#raise Exception(request)
if request.user.is_authenticated():
return HttpResponseRedirect('/home/')
if request.method == 'POST':
form = FuserRegistration(request.POST)
if form.is_valid():
new_user = form.save()
authenticated_user=authenticate(username=form.cleaned_data["username"],password=form.cleaned_data["password"])
login(request, authenticated_user)
return HttpResponseRedirect("/home/")
else:
template=u"registration/login.html"
context = {"form": form}
return render(request, template, context)
#raise Exception(form.errors)
else:
form = FuserRegistration()
template=u"registration/login.html"
context = {"form": form}
return render(request, template, context)
def userLogin(request,template=None,context=None):
if request.user.is_authenticated():
return HttpResponseRedirect('/home/')
if request.method == 'POST':
form = userLoginform(request.POST)
if form.is_valid():
# authenticated_user = form.save()
# login(request, authenticated_user)
return HttpResponseRedirect("/home/")
else:
template=u"registration/login.html"
context= {"form": form}
return render(request, template,context)
else:
form=userLoginform()
template=u"registration/login.html"
context= {"form": form}
return render(request, template,context)
def userLogout(request):
logout(request)
return HttpResponseRedirect("/login")
def home(request):
return render_to_response(
'home.html',
{ 'user': request.user }
)
#@csrf_exempt
#def user_profile(request):
# if request.method == 'POST':
# form = ImageForm(request.POST, request.FILES)
# if form.is_multipart():
# new_user = save_file(request.FILES['fileinput'])
# return HttpResponse(new_user)
# else:
# return HttpResponse("form is not valid")
# else:
# form = ImageForm()
# return render_to_response('user_profile.html', { 'form': form })
# form = UploadFileForm(request.POST, request.FILES)
# if form.is_valid():
# handle_uploaded_file(request.FILES['file'])
# return HttpResponseRedirect('/user_profileform/')
# else:
# return HttpResponse('1')
# getusername = request.POST['username']
# getfirstname = request.POST['first_name']
# getemail = request.POST['email']
# if getusername != '' and getfirstname != '' and getemail !='':
# try:
# getvalue = User.objects.get(username=getusername)
# if getvalue:
# getvalue.username = getusername
# getvalue.first_name = getfirstname
# getvalue.email = getemail
# getvalue.save()
# userexist = "user exist"
# except:
# userexist = "Does not exist"
# else:
# userexist = "require field"
# return HttpResponse(userexist)
def user_profileform(request):
return render_to_response(
'user_profile.html',
{ 'user': request.user }
)
#function for rendering templates
def render(request, templates, dictionary=None, context_instance=None,
**kwargs):
"""
Mimics ``django.shortcuts.render`` but uses a TemplateResponse for
``mezzanine.core.middleware.TemplateForDeviceMiddleware``
"""
dictionary = dictionary or {}
if context_instance:
context_instance.update(dictionary)
else:
context_instance = RequestContext(request, dictionary)
return TemplateResponse(request, templates, context_instance, **kwargs)
@csrf_exempt
def upload_file(request):
getfirstname = ''
getusername = ''
getemail = ''
getpassword = ''
if request.method == 'POST':
getusername = request.POST['username']
getfirstname = request.POST['first_name']
getemail = request.POST['email']
getpassword = request.POST['password']
form = UploadFileForm(request.POST, request.FILES)
# if request.FILES['file'].name:
# filename = request.FILES['file'].name
for filename, file in request.FILES.iteritems():
filename = request.FILES[filename].name
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
# return HttpResponseRedirect('/user_profileform/')
getvalue = User.objects.get(username=getusername)
getvalue.username = getusername
getvalue.first_name = getfirstname
getvalue.email = getemail
getvalue.save()
return render_to_response('user_profile.html', {'form': form, 'getusername': getusername,'getfirstname': getfirstname, 'getemail':getemail, 'getpassword':getpassword })
else:
form = UploadFileForm()
return render_to_response('user_profile.html', {'form': form })
@csrf_exempt
def handle_uploaded_file(f):
destination = open('media/filename', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()
我想在用户登录时打印该值,而不是从登录表单中获取值并打印{{user.username}}