Dave Partner

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Ezechukwu James

Pages: [1]
1
In this tutorial we are going to explore the general structure of a  Django project. We will also look at how to use custom templates and static files like Javascript and CSS files  in your Django project. A typical Django project has a tree structure shown below

The Project
simplebloggerproject
   |
   |__ simplebloggerproject
   |   |__   __init__.py
   |   |__   settings.py
   |   |__   urls.py
   |   |__   wsgi.py
   |
   |__ static
   |    |__ app_name (or project name) - 'namespacing'
   |         |__ css
   |         |__ fonts
   |         |__ images
   |         |__ js
   |   
   |__ manage
   |
   |__ simple_blogger (app name)
   |  |
   |  |__  migrations
   |  |      |__   __init__.py
   |  |   
   |  |__  __init__
   |  |__  admin.py
   |  |__  models.py
   |  |__  tests.py
   |  |__  views.py
   |  |__  urls.py

Download or clone the sample project here >> https://github.com/EzechukwuJI/simplebloggerproject.git
__init__.py  - The __init__.py file in any directory indicates to the Python interpreter that the
directory should be treated like a Python package. in other words, it initializes a python package.  This makes it easy to import files from this directory.
With the __init__.py file present in a directory, we can do something like this >> from directory_name import file_name
*More about  __init__  here >> http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html

2. settings.py - As the name suggests, the settings file holds the default and user defined settings for your project.

3. urls.py - At the project level, the urls.py contains the root url of each application in the project.
It is used to create a root(base) url to which application level urls defined within a particular namespace are appended.
This is better explained with an illustration.

In the simplebloggerproject urls.py file we define a root url for the simple_blogger application as follows
   urlpatterns = [
         url('^',      include('simple_blogger.urls',  namespace  = "simple_blogger")),
          url('^',      include('app2.urls',            namespace  = "app2")),
          url('^',      include('app3.urls',            namespace  = "app3"))
   ]
 and in the app url (using the simple_blogger application), we can define urls as follows;
      urlpatterns = [
              url(r'^posts/recent-posts/$',  views.recentPostView,  name = 'recent-post'),
       ]
With the base urls defined, we can call application level urls in the our templates using
the shortcut like this
             {% url 'app_namespace:url_name' %}, In our example,  {% url 'simple_blogger:recent-post' %}
and in  the views, we can use the built-in reverse function to call the url  using
      reverse('app_namespace:url_name'),  In our example, reverse('simple_blogger:recent-post')

The above line of code return the absolute url string as defined on the left hand side of the url in the app-level urls.py file. In our example we have   'http://domain_name.com/posts/recent-posts/

The goal of using this approach is to ensure loose coupling, which is a core component of the Django design philosophy.
more at >> https://docs.djangoproject.com/en/1.10/misc/design-philosophies/

4. wsgi.py - A Web Server Gateway Interface (WSGI) server implements the  web server side of the WSGI interface for running Python web applications. It directs the server to the  project settings.
more explanation at >> https://www.fullstackpython.com/wsgi-servers.html
5. static - The static directory is where you have all the static files used in your project.
The sub directories are use to store applicable files as indicated.
It is important to group app specific static files and directories inside the app name.
Namespacing helps you stay organized and makes your app portable.

The Application
The other directories are usually for the different apps in the project. In our example - simplebloggerproject, we have
just one application.
By default, a new application has four files, admin.py, models.py, tests.py and views.py. We can add as many files as required.

admin.py - This is where we register models defined in models.py so that they can be viewed in the Django admin interface.

models.py - The models.py file is where we define our database models. Models are Python classes which
inherits from Django's Model class. The classname in the table name in the database while the class attributes are the database
fields in the defined table. See example.
 
 class Students(models.Model):
          name            =   models.CharField(max_length = 200)
          age               =   models.IntegerField(default = 0)
          field_name    =   field_attribute()

After creating the models classes, we have to make migrations. This is done by running the command below in the command line.
         manage.py makemigrations
This action builds a migration file for the models we have created. The database structure is created when we migrate by running
   manage.py migrate
in the command line.
The last two processes are repeated anytime changes are made to the model class(es) to update the database structure.

Migrating the models builds a database schema, usually with the project name as the database name, model classes as tables
and model class attributes as fields in the tables created.

Views.py - The views.py file is where we control what is/isn't visible to a user on our website, perform actions based
on user inputs and action. Views are can be Python classes in Class Based View (CBV) approach of Python functions (methods) in
Function Based View approach.

urls.py - This is user defined - in that, you create the file yourself. The application level urls.py file is
where app urls are defined as explained above.
We can add other files such as custom_functions.py, forms.py, middleware.py where necessary.

Next, we will look at customizing our templates and using static files in our app.

If you have any questions you can drop it in the comment section or on the open Facebook group (link below) for quicker responses.
Join the Facebook group here >> https://www.facebook.com/groups/466737186852664/

Please signup on this site to get alerts as subsequent tutorials are published. Signup here => http://davepartner.com/register/
See you in the next tutorial.





2
THE PROJECT
With Python and Django installed on your system, open your command prompt (for Windows) or bash console (for Mac),
navigate to the directory you want the project to reside using the cd command, then enter the following command.

   django-admin.py startproject <project_name>

in our example we use;
   
   django-admin.py startproject simplebloggerproject

Running this command creates a Django project with the name you specified in the directory you specified. Don't worry if you don't
see any change in your command prompt, it is normal. To see what has changed, go to the directory where you created the project (e.g desktop),  you should see a new folder with the name of the project you just  created.
This folder contains two items - a sub-directory with the same name and a python file with the name 'manage'.
The sub-directory contains four files namely;  __init__, settings, urls and wsgi - all ending with a .py(python's extension).

So far our folder structure looks like this

simplebloggerproject
   |
   |__simplebloggerproject
   |    |__   __init__.py
   |    |__   settings.py
   |    |__   urls.py
   |    |__   wsgi.py
   |
   |__ manage   

We will explain what each of these file does as we progress.

NB: you can download the sample project here >> https://github.com/EzechukwuJI/simplebloggerproject

THE APP
Django is designed to be as flexible and portable as possible, with the DRY (Don't Repeat Yourself) principle
at the core of its development processes.

The project created above is a container for any number of applications you want to create. Each of these
applications have an independent existence and are connected with urls. This makes it interestingly easy to create pluggable micro-components for for the entire project.

In our example, we will create our first application and we will simply call it "simple_blogger".

To create the app, open your command prompt and navigate to the simplebloggerproject project directory, then run the command below.

   C:\Users\Hypermatrix Tech\Desktop\simplebloggerproject> manage.py startapp <app_name>

in our project we'll have

   C:\Users\Hypermatrix Tech\Desktop\simplebloggerproject> manage.py startapp simple_blogger

This creates the simple_blogger App inside the simplebloggerproject  directory.

The App we just created is a directory with six items - a sub-directory with the name "migrations", __init__.py, admin.py,
models.py, tests.py and views.py.
Each of these files have their unique functions. We will add more file as we proceed.

WHAT WE HAVE COVERED SO FAR
If you followed correctly, you should have a project tree similar to the one below.

simplebloggerproject
   |
   |__simplebloggerproject
   |   |__   __init__.py
   |   |__   settings.py
   |   |__   urls.py
   |   |__   wsgi.py
   |
   |__ manage
   |
   |__ simple_blogger
      |__  migrations
      |      |__   __init__
      |
      |__  __init__
      |__  admin
      |__  models
      |__  tests
      |__  views

That's about all we have to do to get our first Django App up and running.

VIEWING THE APP (WEBSITE) ON THE BROWSER
Before we can view what we have so far, we have to complete a few setup.

Open the simplebloggerproject directory and open the settings.py file in any text editor of your choice.
On the settings file, locate the INSTALLED_APP tuple and add the name of the application we just created,

INSTALLED_APPS = (
   '',
   'simple_blogger',
   )
NOTE: Don't forget to add the trailing comma at the end of the App name as shown.

As we create more Apps, we add them to project in the same way. We will explain the other parts of the
settings.py as we add more functionalities to the project.

Next step is to initialize our project by migrating the changes we have made together with the default
settings and db models automatically created at the point of creating the project and the App.

Back to the command prompt and in the simplebloggerproject directory run the following command.
   
   C:\Users\Hypermatrix Tech\Desktop\simplebloggerproject> manage.py migrate

After the migration has completed, we can then start our server by running this command

   C:\Users\Hypermatrix Tech\Desktop\simplebloggerproject> manage.py runserver

After you've started your server, open your browser and on the address bar, enter the following
   
           127.0.0.1:8000

If you followed the steps correctly, you should see a congratulatory message.
By default, Django runs on port 8000. However, you can specify a different port at the point of running the server like this;
   
   manage.py runserver 9000

This will run the server on port 9000, and to access the App, 
enter 127.0.0.1:9000 in the browser's address bar.

Congratulations! you just created your first Django based website

NB: you can download the sample project here >> https://github.com/EzechukwuJI/simplebloggerproject

NEXT STEP
In our next tutorial, we will explain the different parts of the project and application we just created and add more functionalities.


If you have any questions you can drop it in the comment section or on the open Facebook group (link below) for quicker responses.

Join the Facebook group here >> https://www.facebook.com/groups/466737186852664/

Please signup on this site to get alerts as subsequent tutorials are published. Signup here => http://davepartner.com/register/

See you in the next tutorial.

3
Django / Installing Python and Django and Setting Up Your Environment
« on: November 20, 2016, 08:56:16 AM »
The Django web development framework is written with Python. Therefore, to use Django, we have to first install Python and then install Django and other dependencies we will need to setup a system for the task.

For this tutorial, we will be using Python 2.7.* and Django 1.8.

INSTALLING PYTHON
If you are using a Mac, you probably have Python already installed; to confirm, open a bash console and type "Python" hit the enter key, this should open the Python interactive console. Else, you can download Python from the official Python website >> https://www.python.org/downloads/, select the version for your OS and install the most stable version in the 2.7 series.


If you are using a Windows system you may have to perform other setup processes such as updating your Environment Variables with the Python Path, the instructions here >> http://www.howtogeek.com/197947/how-to-install-python-on-windows/  explain it in details.


INSTALLING DJANGO

Now that you have installed Python on your system, next is to install the Django framework and start hacking away immediately. Follow the instructions here >> https://tutorial.djangogirls.org/en/django_installation/ to install Django on your system. It covers both Mac and Windows installations.


Django is a Python package, to test your installation, open your command line (bash console for Mac) and type "Python" and hit the enter key to open the interactive console, then type "import django" to import the Django package and then "django.get_version()" to check the version of Django you have installed.   


NEXT STEP

If you followed the steps correctly, you should be all set to start building web applications using Django. In this tutorial, we will be building a simple blogging site called SimplebloggerProject from scratch, adding functionalities as we progress.
The project repository is at >> https://github.com/EzechukwuJI/simplebloggerproject

See you in the next tutorial.

See you in the next tutorial.
PS: Join the Facebook group here >> https://www.facebook.com/groups/466737186852664/

Please signup on this site to get alerts as subsequent tutorials are published. Signup here => http://davepartner.com/register/


4
Django / Web Development From Basic To Advanced Using Python/Django
« on: November 17, 2016, 12:20:48 AM »
Hey guys!
Given all the buzz about PHP, Laravel, Javascript and the likes, it is easy for newbies or intending developers to think of these languages and framework mentioned as the only ones available for building robust web applications.

Wouldn't it be nice to know and master the language and framework behind some major websites like Dropbox, Quora, and Google.
Well, the language is called Python.

Going further, we have websites like
http://pinterest.com,
The NASA website http://www.science.nasa.gov/
Spotify http://spotify.com
Disqus and Instagram http://instagram.com
And so on.

All of these websites and platforms are built on a very powerful Django framework.

In this section, I will take you through the processes of creating a full database driven website using Django.

Django is a python framework for building powerful and scalable websites. It is lightweight, fast, easy to use, powerful and secure web development framework . It has existed for over 10 years and has a very rich and active community.
Django is free and Open Source.

You can read more about Django here >> https://www.djangoproject.com/

Requirements
1. Basic Knowledge of Python programming language.


To get started with Python, you can download any of the material listed in the link below.
https://medium.mybridge.co/19-free-ebooks-to-learn-programming-with-python-8f6f0ad4a7f8#.aurwzuntw

2. HTML
3. CSS

PS: Join the Facebook group here >> https://www.facebook.com/groups/466737186852664/

See you in the next tutorial.


Please signup on this site to get alerts as subsequent tutorials are published. Signup here => http://davepartner.com/register/

Pages: [1]