Python Django Part 1 - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Python Django Part 1 - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript


Post Top Ad

Post Top Ad

Tuesday, December 18, 2018

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


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
$ startproject learning_log.
#Create a database
$python migrate
#View the project
$ python runserver
#Create a new app
$python startapp learning_logs


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 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 file.

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

python django

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

python django migartion

Creating a superuser A superuser is a user account that has access to all aspects of the project.
$ python 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 file.
from django.contrib import admin

from learning_logs.models import Topic


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 file tells Django where to find the 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 (,
 url(r'', include('learning_logs.urls', namespace='learning_logs')),

python url

Mapping an app's URLs An app's 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 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') 
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'),


python django template


django localhost


python url

python file structure

python django sub


No comments:

Post a Comment

Post Top Ad