1. Welcome
Try DJANGO Tutorial series is here to teach you Django bit by bit.
- Playlist: https://www.youtube.com/playlist?list...
- Code: https://github.com/codingforentrepren...
- Subscribe: http://joincfe.com/youtube
- Pro: http://kirr.co/ni5fia
2. Installing to Get Started
- Github: Try-Django
- StackOverflow
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
网友评论