美文网首页
Try DJANGO Tutorial -- Part I

Try DJANGO Tutorial -- Part I

作者: c8ac4dad76db | 来源:发表于2018-09-06 23:38 被阅读13次

    1. Welcome

    Try DJANGO Tutorial series is here to teach you Django bit by bit.

    2. Installing to Get Started

    3. Setup your Virtual Environment for Django

    mkdir try-django
    cd try-django
    virtualenv .venv  # or:  virtualenv -p python3 .venv
    source .venv/bin/activate
    
    pip install django
    pip freeze > requirements.txt
    

    4. Create a Blank Django Project

    django-admin startproject trydjango .
    

    5. Setup Your Code Text Editor

    • vim / Emacs
    • Atom / Sublime Text / VS Code
    • PyCharm

    6. Settings

    trydjango/settings.py

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    SECRET_KEY = 'et%wd!0vnfr8d9foqfss^q+ds%-m*ge^%0klhxvn4$a3jt1$si'
    
    DEBUG = True
    
    ALLOWED_HOSTS = []
    
    # ...
    

    7. Built-In Components

    python manage.py makemigrations
    python manage.py migrate
    python manage.py createsuperuser  
    

    Access http://127.0.0.1:8000/admin

    8. Your First App Component

    python manage.py startapp products
    python manage.py startapp blog
    python manage.py startapp profiles
    python manage.py startapp cart
    

    trydjango/settings.py:

    INSTALLED_APPS = [
        # ...
    
        'products',
    ]
    

    products/models.py:

    from django.db import models
    
    
    class Product(models.Model):
        title = models.TextField()
        description = models.TextField()
        price = models.TextField()
        summary = models.TextField(default='this is cool!')
    

    products/admin.py:

    from django.contrib import admin
    
    from .models import Product
    
    admin.site.register(Product)
    

    9. Create Product Objects in the Python Shell

    python manage.py shell
    
    (.venv) > $ python manage.py shell
    Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
    [GCC 7.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> from products.models import Product
    >>> Product.objects.all()
    <QuerySet []>
    >>> Product.objects.create(title="New product 1", description="first one", price='19132', summary="sweet")
    <Product: Product object (1)>
    >>> Product.objects.create(title="New product 2", description="another one", price='19132', summary="sweet")
    <Product: Product object (2)>
    >>> Product.objects.create(title="New product 2", description="another one", price='19132', summary="sweet")
    <Product: Product object (3)>
    >>> Product.objects.create(title="New product 2", description="another one", price='19132', summary="sweet")
    <Product: Product object (4)>
    >>> Product.objects.create(title="New product 2", description="another one", price='19132', summary="sweet")
    <Product: Product object (5)>
    >>> Product.objects.create(title="New product 2", description="another one", price='19132', summary="sweet")
    <Product: Product object (6)>
    

    10. New Model Fields

    https://docs.djangoproject.com/en/2.1/ref/models/fields/

    products/models.py:

    from django.db import models
    
    
    class Product(models.Model):
        title = models.CharField(max_length=120)
        description = models.TextField(blank=True, null=True)
        price = models.DecimalField(decimal_places=2, max_digits=10000)
        summary = models.TextField(default='this is cool!')
    
    python manage.py makemigrations
    python manage.py migrate
    

    相关文章

      网友评论

          本文标题:Try DJANGO Tutorial -- Part I

          本文链接:https://www.haomeiwen.com/subject/kxtqgftx.html