💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**View配置示例** ~~~ # _*_ coding:utf-8 _*_ from django.shortcuts import render, get_object_or_404 #get_object_or_404避免请求函数错误 from .models import Blogs #引入app models # Create your views here. def blog_title(request):#request 负责接收请求 blogs = Blogs.objects.all() #return 结束当前参数。render()的作用是将数据渲染到指定模版上,blogs":blogs 第一个blogs,为指定页面接收参数 return render(request, 'blog/titles.html', {"blogs":blogs}) def blog_article(request, article_id): # article_id是(titles.html文件blog.id参数传入接收变量),id为Blogs数据表 #article = Blogs.objects.get(id=article_id) article = get_object_or_404(Blogs, id=article_id) pub = article.publish #article、publishe为渲染到content.html变量名 return render(request, 'blog/content.html', {"article":article, "publishe":pub}) ~~~ **View配置扩展** 1、增加点击次数 ~~~ def BlogsView(request, pk): blogs = get_object_or_404(Blog, pk=pk) #增加点击次数 blogs.click blogs.click += 1 #save()保存致数据库 blogs.save()保存致数据库 return render(request,"blog1.html", { "blogs":blogs }) ~~~ 2、用户登录、注册、判断 ~~~ # _*_ coding:utf-8 _*_ from django.shortcuts import render, redirect #引入系统模块 from django.http import HttpResponse #引入内置用户登录管理 from django.contrib.auth import authenticate, login #引入内置用户数据 from django.contrib.auth.models import User #引入forms类 from .forms import LoginFrom, RegisterFrom, UserInfoForm #引入models from .models import UserInfo #新增用户信息,required引入装饰器,判断用户登录,如果没有登录跳转其他页面 from django.contrib.auth.decorators import login_required # Create your views here. #用户登录 def user_login(request): #method是HttpRequest对象的一个常用属性,可返回POST、GET if request.method == "POST": login_form = LoginFrom(request.POST) #is_valid验证传入数据是否合法性 if login_form.is_valid(): #cleaned_data引用一个字典类型数据 cd = login_form.cleaned_data #authenticate检验数据库帐号密码是否正确 user = authenticate(username=cd['username'], password=cd['password']) #login引入的login()函数,已语句user实例对象作为参数,实现用户登录,登录后调用session if user: #判断正确返回HttpResponse login(request, user) #redirect跳转 return redirect("login:me") #return HttpResponse("欢迎您的到来") else: return HttpResponse("帐号密码错误") else: return HttpResponse("帐号密码错误") if request.method == "GET": login_form = LoginFrom() return render(request, "users/login.html", {"form": login_form}) #用户注册 def register(request): if request.method == "POST": user_form = RegisterFrom(request.POST) #新增字段 userprofile_form = UserInfoForm(request.POST) #新增*userprofile_form.is_valid()验证数据 if user_form.is_valid()*userprofile_form.is_valid(): new_user = user_form.save(commit=False) new_user.set_password(user_form.cleaned_data['password']) new_user.save() #新增字段 new_profile = userprofile_form.save(commit=False) new_profile.user = new_user new_profile.save() return redirect("login:user_login") #return HttpResponse("successfully") else: return HttpResponse("密码设置错误,请重新注册。") else: user_form = RegisterFrom() return render(request, "users/register.html", {"form": user_form, "profile": UserInfoForm}) #用户如果没有登录跳转至登录页面 @login_required(login_url='login:user_login') #用户信息 def myself(request): user = User.objects.get(username=request.user.username) userinfo = UserInfo.objects.get(user=user) return render(request, "users/me.html", {"user": user, "userinfo": userinfo}) ~~~ 3、数据筛选.objects.all().order_by、objects.filter ~~~ # _*_ coding:utf-8 _*_ from django.shortcuts import render, get_object_or_404 from .models import fengg, qyfl, qy, sx, chanpin # Create your views here. #导航 def Fengg(request): chanpinx = chanpin.objects.all().order_by('-id') fenggx = fengg.objects.all() qyx = qy.objects.all() sxx = sx.objects.all() return render(request, 'best1/best.html', {"chanpinx": chanpinx, "fenggx": fenggx, "qyx": qyx, "sxx": sxx}) #风格分类 def chanpin_names(request, pk): #pk传入fengg数据 cate = get_object_or_404(fengg, pk=pk) #fengg 为chanpin数据表ForeignKey所对应数据 #filter 筛选数据 post_list = qyfl.objects.filter(title=cate) #导航分类 fenggx = fengg.objects.all() qyx = qy.objects.all() sxx = sx.objects.all() return render(request, 'best1/cp.html', {"post_list": post_list, "fenggx": fenggx, "qyx": qyx, "sxx": sxx}) ~~~ titles.html样板 ~~~ {#继承base.html文件#} {% extends "base.html" %} {% block title %}文章页面{% endblock %} {% block content %} {% for blog in blogs %} {# blog变量#} {# blogs为views.py文件传入参数blogs#} <li><a href="{{ blog.id }}">{{ blog.title }}</a></li> {% endfor %} {% endblock %} ~~~ content.html样板 ~~~ {#继承base.html文件#} {% extends "base.html" %} {% block title %}文章第二页面{% endblock %} {% block content %} {#article、publishe是views传入变量名参数,title、author、body是models数据表,username是User内置数据表#} <h1>{{ article.title }}</h1> {{ article.author.username }} {{ publishe }} {{ article.body }} {% endblock %} ~~~