Python Django Part 1

Python Django Part 1




What is Django?

Django is a web framework which helps you build interactive websites using Python. With Django you define the kind of data your site needs to work with, and you define the ways your users can work with that data.

Installing Django

It's usually best to install Django to a virtual environment, where your project can be isolated from your other Python projects. Most commands assume you're working in an active virtual environment.



#Create a virtual environment
$ python -m venv 11_env
#Activate the environment (Linux and OS X)
$ source 11_env/bin/activate
#Activate the environment (Windows)
> 11_env\Scripts\activate
#Install Django to tghe active environment
(11_env)$ pip install Django

Screenshot 

python django

 

python django env

 

python django env

 

python django env

 


Creating a project

To start a project we'll create a new project, create a database, and start a development server.


#Create a new project
$ django-admin.py startproject learning_log.
#Create a database
$python manage.py migrate
#View the project
$ python manage.py runserver
#Create a new app
$python manage.py startapp learning_logs

Screenshot 

python django migration
python django run server

 
python django server

python django model



Working with models

The data in a Django project is structured as a set of models.


#Defining a model
#To define the models for your app, modify the file models.py that was created in your app's folder. The __str__() method tells Django how to represent #data objects based on this model.

from  django.db import models
from django.db import models

# Create your models here.
class Topic(models.Model):
 """A topic the user is learning about."""
 text = models.CharField(max_length=200)
 date_added = models.DateTimeField(auto_now_add=True)

 def __str__(self):
   return self.text



python model




Activating a model To use a model the app must be added to the tuple INSTALLED_APPS, which is stored in the project's setting.py file.
INSTALLED_APPS = (
--snip--
'django.contrib.staticfiles',

# My apps 
'learning_logs',
)
Set The Template Path in setting.py
'DIRS': [os.path.join(BASE_DIR, 'templates')],

python django setting.py


Migrating the database The database needs to be modified to store the kind of data that the model represents.
$ python mange.py makemigrations learning_logs
$ python manage.py migrate


python django migartion

Creating a superuser A superuser is a user account that has access to all aspects of the project.
$ python manage.py createsuperuser

python django superuser


Registering a model You can register your models with Django's admin site, which makes it easier to work with the data in your project. To do this, modify the app's admin.py file.
from django.contrib import admin

from learning_logs.models import Topic

admin.site.register(Topic)


python admin.py


Building a simple home page 

Users interact with a project through web pages, and a project's home page can start out as a simple page with no data. A page usually needs a URL, a view, and a template.

Mapping a project's URLs
The project's main urls.py file tells Django where to find the urls.py files associated with each app in the project.
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
 url(r'^admin/', include (admin.site.urls)),
 url(r'', include('learning_logs.urls', namespace='learning_logs')),
]

python url


Mapping an app's URLs An app's urls.py file tells Django which view to use for each URL in the app, You'll need to make this yourself, and save it in the app's folder
from django.conf.urls import url

from . import views 

urlpatterns = [
 url(r'^$', views.index, name='index'),
]


python url

Writing a simple view A view takes information from a request and sends data to the browser, often through a template. View function are stored in an app's views.py file. This simple view function doesn't pull in any data, but it uses the template index.html to rernder the home page.
from django.shortcuts import render

def index(request):
 """The home page for Learning Log."""
 return render(request, 'learning_logs/index.html') 
Final urls.py
from django.conf.urls import include, url
from django.contrib import admin
import sys
sys.path.insert(0, '/home/sanjeev/python/learning_log/learning_logs/')
import views
urlpatterns = [
 url(r'^$', views.index, name='index'),
]

Screenshot 

python django template

 

django localhost

 

python url

python file structure
 

python django sub

 

0 comments:

Post a Comment