十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要介绍了django的环境配置和view的使用 ,具有一定借鉴价值,需要的朋友可以参考下。步骤简单适合新手,希望你能收获更多。下面是配置和使用的步骤内容。
成都创新互联是专业的武城网站建设公司,武城接单;提供成都做网站、成都网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行武城网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
mkdir djanad cd djanad/ pyenv virtualenv 3.6.5 djanad pyenv local djanad
结果如下
pip install django==2.1
django-admin startproject demo . django-admin startapp app
结果如下
数据库配置如下
基本时区和MySQL配置及相关时区配置请看django基础
https://blog.51cto.com/11233559/2444627
启动结果如下
django内置了自己的模板引擎,和jinjia 很像,使用简单
使用 Template 进行定义模板,使用Context 将数据导入到该模板中,其导入默认使用字典
django 默认会去到app_name/templates下寻找模板,这是settings中的默认设置,默认会去app_name/static找那个寻找静态文件(css,js,jpg,html)等
在 app/models.py 中创建数据库表模板,具体配置如下:
from django.db import models # Create your models here. # 问题 class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __str__(self): return self.question_text # 选择 # 配置选择为问题的外键,并配置选择的内容和选择的起始值 class Choice(models.Model): question = models.ForeignKey(Question, on_delete=Question) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) def __str__(self): return self.choice_text
python manage.py makemigrations python manage.py migrate
结果如下
创建后台登陆用户,设置用户名为admin,密码为admin@123
app/admin.py中添加
# Register your models here. from django.contrib import admin from .models import Question, Choice # Register your models here. class ChoiceInline(admin.TabularInline): model = Choice extra = 3 class QuestionAdmin(admin.ModelAdmin): fieldsets = [ (None, {'fields': ['question_text']}), ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}), ] inlines = [ChoiceInline] list_display = ('question_text', 'pub_date') admin.site.register(Choice) admin.site.register(Question, QuestionAdmin)
url : localhost:port/admin/
demo/setting.py 中配置添加
STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static') ]
项目中创建static 并上传图片django.jpg
demo/urls.py中配置如下
from django.conf.urls import url, include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^app/', include("app.urls",namespace="app")), #此处配置名称空间,用于处理后面的翻转 ]
from django.conf.urls import url, include from . import views urlpatterns = [ url(r'^index/$', views.index, name="index"), # name 指定名称, ]
from django.shortcuts import render from django.template import Template, Context from . import models from django.http import HttpResponse # Create your views here. def index(request): lastes_question_list = models.Question.objects.order_by('-pub_date')[:5] template = Template(""" {% if lastes_question_list %} {% for question in lastes_question_list %} - {{ question.question_text }}
{% endfor %}
{% endif %} """) context = Context({"lastes_question_list": lastes_question_list}) return HttpResponse(template.render(context))
访问配置,结果如下
index 代码如下
测试数据 {% if lastes_question_list %} {% for question in lastes_question_list %} - {{question.question_text}}
{% endfor %}
{% endif%}
app/view.py 中代码如下
from . import models from django.http import HttpResponse from django.template import loader # Create your views here. def index(request): lastes_question_list = models.Question.objects.order_by('-pub_date')[:5] template = loader.get_template("app/index.html") context = {"lastes_question_list": lastes_question_list} return HttpResponse(template.render(context))
from . import models from django.shortcuts import render # Create your views here. def index(request): lastes_question_list = models.Question.objects.order_by('-pub_date')[:5] context = {"lastes_question_list": lastes_question_list} return render(request, template_name="app/index.html", context=context)
根据根路由中注册的namespace和子路由中注册的name来动态获取路径。在模板中使用"{% url namespace:name %}"
如果携带位置参数
“{% url namespace:name args %}"
如果携带关键字参数
“{% url namespace:name k1=v1 k2=v2 %}"
配置 详情页面添加数据
app/view.py 中添加数据如下
from . import models from django.shortcuts import render # Create your views here. def index(request): lastes_question_list = models.Question.objects.order_by('-pub_date')[:5] context = {"lastes_question_list": lastes_question_list} return render(request, template_name="app/index.html", context=context) def detal(request, question_id): detal = models.Question.objects.get(pk=question_id) context = {"detal": detal} return render(request, template_name="app/detal.html", context=context)
app/urls.py中如下
from django.conf.urls import url, include from . import views urlpatterns = [ url(r'^index/$', views.index, name="index"), url(r'^(?P[0-9]+)/$', views.detal, name="detal"),# name 指定名称,用于后面的反向解析 ] ]
详情页html 配置如下
测试数据 {% if detal %} {{ detal.question_text }}
{% for question in detal.choice_set.all %} {{ question.votes }} {{ question.choice_text }} {% endfor %} {% endif %}
index.html 修改如下
{% load static %} 测试数据 {% if lastes_question_list %} {% for question in lastes_question_list %} - {{question.question_text}}
{% endfor %}
{% endif%}
看完上述内容,你们掌握django的环境配置和view的使用方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!