zoukankan      html  css  js  c++  java
  • Django2第一个工程

    开发Django应用的典型工作流程是:先创建模型,接着最先让管理页run起来,这样你的同事或者客户就可以开始往系统里填数据。 然后,开发如何将数据展示给公众的部分。
    
    创建一个项目:
    django-admin startproject mysite
    
    进入mysite目录,启动开发服务器:
    $ python manage.py runserver
    访问:http://127.0.0.1:8000
    指定端口:
    $ python manage.py runserver 8080
    
    创建应用:
    $ python manage.py startapp polls
    
    polls/views.py:
    from django.http import HttpResponse
    def index(request):
        return HttpResponse("Hello, world. You're at the polls index.")
    
    要调用视图,我们需要将它映射到一个URL.创建polls/urls.py:
    from django.urls import path
    from . import views
    urlpatterns = [
        path('', views.index, name='index'),
    ]
    
    下一步是将根URLconf指向polls.urls模块。mysite/urls.py:
    from django.urls import include, path
    from django.contrib import admin
    urlpatterns = [
        path('polls/', include('polls.urls')),
        path('admin/', admin.site.urls),
    ]
    
    $ python manage.py runserver
    在浏览器中转到http://localhost:8000/polls/,您应该看到文本“Hello, world. You’re at the polls index.“
    
    
    
    mysite/settings.py配置数据库
    配置时区、语言:
    LANGUAGE_CODE = 'zh-Hans'
    TIME_ZONE = 'Asia/Shanghai'
    
    请注意文件顶部的INSTALLED_APPS设置。 它包含在此Django实例中激活的所有Django应用程序的名称。
    其中一些应用程序至少使用了一个数据库表,所以我们需要在数据库中创建表格,然后才能使用它们。
    $ python manage.py migrate
    
    migrate命令查看INSTALLED_APPS设置,并根据mysite/settings.py文件中的数据库设置创建所有必要的数据库表,迁移命令将只对INSTALLED_APPS中的应用程序运行迁移。
    
    
    
    polls/models.py:
    from django.db import models
    class Question(models.Model):
    	question_text = models.CharField(max_length=200)
    	pub_date = models.DateTimeField('date published')
    class Choice(models.Model):
    	question = models.ForeignKey(Question, on_delete=models.CASCADE)
    	choice_text = models.CharField(max_length=200)
    	votes = models.IntegerField(default=0)
    
    要将该应用程序包含在我们的项目中,我们需要在INSTALLED_APPS设置中添加对其配置类的引用。
    mysite/settings.py:
    INSTALLED_APPS = [
        'polls.apps.PollsConfig',
    ]
    
    $ python manage.py makemigrations polls
    通过运行makemigrations,您告诉Django您已经对模型进行了一些更改,并且您希望将更改存储为一个migration。
    
    sqlmigrate命令使用迁移名称并返回它们的SQL:
    $ python manage.py sqlmigrate polls 0001
    
    再次运行migrate以在您的数据库中创建这些模型表:
    $ python manage.py migrate
    
    
    请记住进行模型更改的三步指南:
    更改模型(在models.py中)。
    运行python manage.py makemigrations为这些更改创建迁移
    运行python manage.py migrate,将这些更改应用到数据库。
    
    
    进入交互式Python shell并使用Django提供的API。
    $ python manage.py shell
    from polls.models import Question,Choice
    from django.utils import timezone
    Question.objects.all()
    q = Question(question_text="What's new?", pub_date=timezone.now())
    q.save()
    q.id
    Question.objects.filter(id=1)
    Question.objects.filter(question_text__startswith='What')
    current_year = timezone.now().year
    Question.objects.get(pub_date__year=current_year)
    
    向模型中添加__str__()方法很重要,不仅为了您在处理交互提示时的方便,还因为在Django自动生成的管理中使用了对象表示。
    
    
    创建管理员账户:
    $ python manage.py createsuperuser
    
    http://127.0.0.1:8000/admin
    
    在admin中修改polls应用程序,polls/admin.py:
    from django.contrib import admin
    from .models import Question
    admin.site.register(Question)
    
    创建polls/templates文件夹
    mysite/settings.py中TEMPLATES:
     'APP_DIRS': True,
    
     创建polls/templates/polls/index.html:
     render()函数将request对象作为第一个参数,将模板名称作为第二个参数,将字典作为可选的第三个参数。返回HttpResponse对象。
    
     get_object_or_404()函数将Django模型作为其第一个参数和任意数量的关键字参数传递给模型管理器的get()函数。 It raises Http404 if the object doesn’t exist.
     还有一个get_list_or_404()函数,它的工作方式类似get_object_or_404()  —— 差别在于它使用filter()而不是get()。 It raises Http404 if the list is empty.
    
    将命名空间添加到您的URLconf中.polls/urls.py:
    app_name = 'polls'
    
    polls/templates/polls/index.html:
    <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    
    you should always return an HttpResponseRedirect after successfully dealing with POST data.
    
    默认情况下,DetailView通用视图使用名为app 名称/ model 名称_detail.html的模板。
    
    
    运行测试用例polls/tests.py:
    $ python manage.py test polls
    
    What happened is this:
    python manage.py test polls looked for tests in the polls application
    it found a subclass of the django.test.TestCase class
    it created a special database for the purpose of testing
    it looked for test methods - ones whose names begin with test
    in test_was_published_recently_with_future_question it created a Question instance whose pub_date field is 30 days in the future
    … and using the assertIs() method, it discovered that its was_published_recently() returns True, though we wanted it to return False
    
    
    Django provides a test Client to simulate a user interacting with the code at the view level.
    
    Good rules-of-thumb include having:
    a separate TestClass for each model or view
    a separate test method for each set of conditions you want to test
    test method names that describe their function
    
  • 相关阅读:
    ThinkPHP Model+数据库的切换使用
    关于SSD安装系统的一些设置(PE安装win 7)
    PHP实现文件下载:header
    Thinkphp 使用PHPExcel导入,栗子
    Ueditor 的使用(这里以php+ci为例)
    js获取鼠标选中的文字内容
    WNMP 下 Nginx 配置 (使用了phpfind一键安装环境)
    javascript 实现 trim
    javascript 获取 CSS 样式表属性
    javascript 删除节点问题
  • 原文地址:https://www.cnblogs.com/liuliu3/p/10812046.html
Copyright © 2011-2022 走看看