• Django入门


    Django入门

    学习了第一章《跟老齐学Python Django实战》 齐伟 编著

    1,创建项目
    django-admin startproject mysite
    或者PyCharm新建DJango项目
    
    2,与项目交互
    python manage.py 回车,可以看到交互的内容
    
    python manage.py startserver   # 启动程序
    修改端口
    python manage.py startserver 9999
    
    3,创建应用
    python manage.py startapp blog
    或者django-admin startapp blog
    
    4,修改settings.py
    添加应用 'blog',
    修改编码 zh-hans
    
    5,添加blog/models.py中的内容
    
    from django.db import models
    from django.utils import timezone
    from django.contrib.auth.models import User
    
    class BlogArticles(models.Model):
    	title = models.CharField(max_length=300)
    	author = models.ForeignKey(User, related_name="blog_posts")
    	body = models.TextField()
    	publish = models.DateTimeField(default=timezone.now)
    
    	class Meta:
    		ordering = ("-publish",)
    
    	def __str__(self):
    		return self.title
    
    需要pip install pytz 来使用timezone
    
    6,根据模型进行数据库迁移
    python manage.py makemigrations       # 数据库迁移准备
    
    python manage.py sqlmigrate blog 0001  # 查看sql语句
    
    python manage.py migrate              # 进行迁移
    
    7,创建超级用户
    python manage.py createsuperuser
    
    进行启动测试;
    
    8,修改 blog/admin.py
    
    from .models import BlogArticles
    
    admin.site.register(BlogArticles)
    
    进行启动测试;
    
    9,修改 blog/admin.py增加效果
    
    from .models import BlogArticles
    
    class BlogArticlesAdmin(admin.ModelAdmin):
    	list_display = ("title", "author", "publish")
    	list_filter = ("publish","author")
    	search_fields = ("title","body")
    	raw_id_fields = ("author",)
    	date_hierarchy = "publish"
    	ordering = ['publish', 'author']
    
    admin.site.register(BlogArticles, BlogArticlesAdmin)
    
    10,修改 blog/views.py,增加视图函数
    
    from .models import BlogArticles
    
    def blog_title(request):
    	blogs = BlogArticles.objects.all()
    	return render(request, "blog/titles.html", {"blogs":blogs})
    
    11,编写模板页面,注意文件需要存储为utf-8格式;
    templates/base.html 
    <!DOCTYPE html>
    <html lang="zh-cn">
    <head>
    	<meta http-equiv="X-UA-Compatible" content="IE=Edge">
    	<meta charset="utf-8">
    	<meta name="viewport" content="width=device-with, initial-scale=1">
    	<title>{%block title%}{%endblock%}</title>
    	<link rel="stylesheet" href="http://necolas.github.io/normalize.css/">
    	<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
    </head>
    <body>
    	<div class="container">
    		{%block content%}
    		{%endblock%}
    	</div>
    	<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
    	<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </body>
    
    </html>
    templates/blog/index.html  # 必须加个blog文件夹,否则不同app下面的index.html页面会重复
    
    {% extends "base.html" %}
    {% block title %}blog titles{% endblock %}
    {% block content %}
    <div class="row text-center vertical-middle-sm">
    	<h1>我的博客</h1>
    </div>
    <div class="row">
    	<div class="col-xs-12 col-md-8">
    		<ul>
    		{% for blog in blogs %}
    			<li>{{blog.title}}</li>
    		{% endfor %}
    		</ul>
    	</div>
    	<div class="col-xs-6 col-md-4">
    		<h2>广告</h2>
    		<p>跟老齐学:www.itdiffer.com</p>
    		<img width="200px" src="http://www.mrwallpaper.com/wallpapers/Fluffy-Cat.jpg">
    	</div>
    </div>
    {% endblock %}
    
    
    12,修改urls.py,引入blog.urls
    from django.conf.urls import url, include
    
    	url(r'^blog/',include('blog.urls', namespace='blog', app_name='blog')),
    
    13,修改titles.html,增加文件详情标签
    <li><a href="{{blog.id}}">{{blog.title}}</a></li>
    
    14,增加详情模板blog/content.html
    {% extends "base.html" %}
    {% block title %}blog article{% endblock %}
    {% block content %}
    <div class="row text-center vertical-middle-sm">
    	<h1>{{article.title}}</h1>
    </div>
    <div class="row">
    	<div class="col-xs-12 col-md-8">
    		<p class="text-center"><span>{{article.author.username}}</span><span style="margin-left:20px">{{publish}}</span></p>
    		<div>{{article.body}}</div>
    	</div>
    	<div class="col-xs-6 col-md-4">
    		<h2>广告</h2>
    		<p>跟老齐学:www.itdiffer.com</p>
    		<img width="200px" src="http://www.mrwallpaper.com/wallpapers/Fluffy-Cat.jpg">
    	</div>
    </div>
    {% endblock %}
    
    15,修改urls.py,引入content.html
    	url(r'(?P<article_id>d)/$', views.blog_article, name="blog_detail")
    
    16,在views.py中引入get_object_or_404
    from django.shortcuts import render, get_object_or_404
    修改获取对象的方法:
    article = BlogArticles.objects.get(id=article_id) 修改为
    article = get_object_or_404(BlogArticles, id=article_id)
  • 相关阅读:
    如何理解cat的输入输出重定向
    Vagrant网络设置
    Vagrant入门
    设计模式的原则
    单例模式
    LRU 实现缓存
    java注解
    java8---lambda表达式
    JUC--Callable 以及Lock同步锁
    JUC--闭锁 CountDownLatch
  • 原文地址:https://www.cnblogs.com/stono/p/8970356.html
Copyright © 2020-2023  润新知