Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,898 members, 7,802,895 topics. Date: Saturday, 20 April 2024 at 02:20 AM

Learn How To Create Websites With Python(Django Web Framework) - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / Learn How To Create Websites With Python(Django Web Framework) (5784 Views)

Python With Django Web Development Training using Real World Projects! / Why Django Is The Best Web Framework For Your Project / Let's Build A Simple Blog With Python(django) (2) (3) (4)

(1) (2) (3) (4) (Reply) (Go Down)

Re: Learn How To Create Websites With Python(Django Web Framework) by MichelleObama: 1:32pm On Mar 28, 2020
ColeDrain:


Have you solved the problem yet?
Thanks the Boss, my pc battery is flat now , will rejoin as soon as possible that notwithstanding, I am following through my phone

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 2:31pm On Mar 28, 2020
MichelleObama:

Thanks the Boss, my pc battery is flat now , will rejoin as soon as possible that notwithstanding, I am following through my phone

Like your determination. Out of power too, will get back as soon as I can.
Re: Learn How To Create Websites With Python(Django Web Framework) by Nobody: 6:50pm On Mar 28, 2020
Following

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by ehix89(m): 8:05pm On Mar 28, 2020
Nice thread, following.....

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 11:50pm On Mar 28, 2020
POST 9

Now we have created a view

Now let's create some urlpatterns to be matched.

open the mysite/urls.py file and update it as follows...

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('polls.urls')),
]


This code path('', include('polls.urls')) from above will redirect everything that comes into 'http://127.0.0.1:8000/' to polls.urls(our app)

The point being that each app has its own url patterns(for now we have only one app).

Go to the polls directory and create a urls.py file, open it in your text editor and add these code..
from django.urls import path
from . import views

urlpatterns = [
path('', views.index, name='index'),
]


So once we have a url that matches the pattern above it calls the index view we created in POST 8(do u remember?)

Go to your command prompt and run the server again by entering:
py manage.py runserver

Then go to your browser and go to http://127.0.0.1:8000/, you should see Hello World. You are doing well as we described in the view function we created.

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 12:28am On Mar 29, 2020
POST 10
let's create another view and urlpattern so we get the working idea.

Go to the polls/views.py file and add this code below other codes

def about(request):
return HttpResponse("This is the about page."wink


please convert the emoji to right closing bracket -)



We have created another view i.e another function that will do something else.

After creating the view, we create a urlpattern to match it(you get the gist now?)

Go to the polls/urls.py file and add this code
path('about/', views.about, name='about'),
, so you should now have this:
notice the part in bold font.

from django.urls import path
from . import views

urlpatterns = [
path('', views.index, name='index'),
path('about/', views.about, name='about'),
]


So the part in bold means any url that ends with about/ should be sent to the about view we created.

Don't forget to save your files after making any changes to them.

Go to your browser and enter 127.0.0.1:8000/about/

You should see- This is the about page

I enjoin you to try and create your own view function and urlpattern, enjoy.

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by thug: 3:14pm On Mar 29, 2020
Everything works perfect. Thanks so much, how about when I shut my computer how to I run the Django server??
Re: Learn How To Create Websites With Python(Django Web Framework) by Abcruz(m): 6:59pm On Mar 29, 2020
@ColeDrain
Thanks bro may you be richly blessed for this selfless service.

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by MichelleObama: 7:03pm On Mar 29, 2020
ColeDrain:


Have you solved the problem yet?

Yes, everything works fine,much thanks
To create urls.py, just go to the text editor, open 'New' and save as URLs.py inside polls app created

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:37pm On Mar 29, 2020
thug:
Everything works perfect. Thanks so much, how about when I shut my computer how to I run the Django server??

- Open your command prompt
- Activate your virtual environment by entering this command: env\Scripts\activate
- change into the mysite directory, type
cd mysite and press enter
- Then type py manage.py runserver to run the server..
Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:38pm On Mar 29, 2020
MichelleObama:


Yes, everything works fine,much thanks
To create urls.py, just go to the text editor, open 'New' and save as URLs.py inside polls app created

Yeah correct, but note urls.py(in small letters)
Re: Learn How To Create Websites With Python(Django Web Framework) by Tolzy11(m): 11:26pm On Mar 29, 2020
ColeDrain:
Now that we are at home.. Instead of whiling away time.. We could pick up a skill. So I am offering to show us how we can create websites using Python programming language through the Django web framework..

Prerequisites:
1. Know fundamentals of Python programming language.
2. Knowledge of HTML, CSS, JS(not really needed).
3. Your dedication, patience, interest and support..
4. A little knowledge of how the web works(I may explain this tho)

Oh! Yes, your laptops or PC's are needed to follow along.

If I can get at least ten interested folks I will start, please do indicate if interested, I am all out for you guys..

P:S
We will create a:
1. Poll website
2. Nairaland clone
3. Chat website
4. Facebook clone
Can you create a WhatsApp group or telegram group please

1 Like 1 Share

Re: Learn How To Create Websites With Python(Django Web Framework) by masterfactor(m): 7:56am On Mar 30, 2020
Nice job

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:59am On Mar 30, 2020
Tolzy11:

Can you create a WhatsApp group or telegram group please

Sounds cool.
Okay I will do that now


P:S
For some reasons I won't create a group now, maybe in the future
Re: Learn How To Create Websites With Python(Django Web Framework) by Abcruz(m): 10:15am On Mar 30, 2020
ColeDrain:


Sounds cool.
Okay I will do that now

Nairaland would have been better so that new comers can benefit from the knowledge of the previous comments.
Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 10:30am On Mar 30, 2020
Abcruz:


Nairaland would have been better so that new comers can benefit from the knowledge of the previous comments.

Hmm, you make a point, and WhatsApp may be a bit stressful esp using mobile(for me to type codes...)

I will stick to nairaland guys

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 12:09pm On Mar 30, 2020
POST 11

Now that we understand how view and urlpatterns work, let's move on.

Feel free to ask your questions(I don't bite)

Things we have learnt so far: urlpatterns, views, how the web works...

Let's talk about DATABASE
Database is simply a collection of information e.g users details like name, sex, favorite quote, all these information are stored in a database. Except your website is static(doesn't change at all), you need to have a database. And for you to manage your database you need a Database Management System(DBMS) that's when you hear SQL- postgresql, mysql, sqlite, mariadb, luckily for us Python comes with sqlite automatically, so we don't need to do any database configuration for now(phew).

One sweet thing about django is that you don't need to write SQL codes, by the way SQL stands for Structured Query Language, SQL helps us communicate with the database, for example to retrieve a User's name or picture or post.. U get?? As I was saying, Django saves us the stress of writing SQL by the means of what we call models- Model in simple terms is like the layout or representation of something.

So how does Django do it?? You may have heard of OOP- Object Oriented Programming which Python supports, OOP simply means model grin
Let me give a basic example, if we want to model a Human, a Human has properties such as- skin color, sex, height and a Human can walk, sleep, eat etc... U get??

Let me stop here, so things don't get too long

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 1:06pm On Mar 30, 2020
POST 11- CONTINUED
So when we model, we create objects and objects have properties and actions(just remember this)

We are creating a poll website-
A poll allows people vote for their preferred answer to a question.

e.g What's the best social network?
- Facebook
- Twitter
- Instagram
Every poll must have a question and choices... So we are going to have two models Question and Choice

Go to polls/models.py file and type in this:

1.from django.db import models

2.class Question(models.Model):
3. question_text = models.CharField(max_length=200)
4. pub_date = models.DateTimeField('date published')

5.class Choice(models.Model):
6. question = models.ForeignKey(Question, on_delete=models.CASCADE)
7. choice_text = models.CharField(max_length=200)
8. votes = models.IntegerField(default=0)


shocked, what the heck is that?? That is how we create models in django or OOP in general.
let me explain the code
- Line 1 was to import a file called models that will help us in modelling
- Line 2&5 defines our model Question and Choice
- Line 3 and 4 are the properties of a question.
Every question must have a text e.g 'What's the best social network?',
Line 4 stores the date when the question was posted on the website.
models.CharField – this is how you define text with a limited number of characters, notice
there's a max_length, we don't want very long questions
models.DateTimeField – this is how you define a date and time field
models.ForeignKey - this helps us to link models together, we know that every question must have a choice, so this is how we link choices to a question.
models.IntegerField - this helps us store number of votes to a choice, we set it's default value to 0, since no one has voted yet.

Save the file.
We have successfully created two models, now we need to add it to our database(sqlite)

First go to mysite/settings.py look for the line INSTALLED_APPS and add 'polls'

INSTALLED_APPS = [
'polls',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

After creating an app always add it to installed apps

Now run this two commands in the command prompt one by one:
py manage.py makemigrations polls
py manage.py migrate

py manage.py makemigrations tells Django that we've made some changes to our models.py

py manage.py migrate implements the changes we've made. You get??


You have successfully added your models to database, kudos.

3 Likes

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 2:12pm On Mar 30, 2020
POST 12
Things we've learned so far: views, urlpatterns, models, how the web works

It ain't so easy, that's the way it feels at first, with dedication you will begin to enjoy it.

Now we've created a model- a representation of a poll website.
How do we add polls(questions and choices)??

I have been saying Django saves time, the way Django does this is by not reinventing the wheel, Django comes with an admin web app(follow come), it is this admin web app we will use to add questions and choices.

First we’ll need to create a user who can login to the admin site. Run the following command in your command prompt:
py manage.py createsuperuser

Enter your desired username and press enter.
username: enter any name
Enter your desired email address
email: enter an email
Enter password(note, the password won't show anything, just type)

Superuser created successfully.

Now start the server by entering: py manage.py runserver
Go to your browser and type in: http://127.0.0.1:8000/admin/
You should see the Django Admin Login Page. Login with your username and password(hope you never forget am). Everything you see there was provided by Django automatically.

Okay, but I can't see a place to add questions or choices, you said..
Well we need to register the polls app in the admin.
How do we do this??

Go to your text editor and open polls/admin.p file and update as shown below:
from django.contrib import admin

from .models import Question

admin.site.register(Question)


save the file
We have successfully registered the Polls app...


Go back to your browser and refresh you should see the Questions model.
Try and add some questions and save.

Take a moment to explore the Admin site

3 Likes

Re: Learn How To Create Websites With Python(Django Web Framework) by castrosteve: 8:37pm On Mar 30, 2020
Nice one .... Fire on bro

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by thug: 8:42pm On Mar 30, 2020
Am getting it all right. Just waiting to finish the poll app. Thanks @coledrain

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by Mentoli: 6:31am On Mar 31, 2020
ColeDrain:
Now that we are at home.. Instead of whiling away time.. We could pick up a skill. So I am offering to show us how we can create websites using Python programming language through the Django web framework..

Prerequisites:
1. Know fundamentals of Python programming language.
2. Knowledge of HTML, CSS, JS(not really needed).
3. Your dedication, patience, interest and support..
4. A little knowledge of how the web works(I may explain this tho)

Oh! Yes, your laptops or PC's are needed to follow along.

If I can get at least ten interested folks I will start, please do indicate if interested, I am all out for you guys..

P:S
We will create a:
1. Poll website
2. Nairaland clone
3. Chat website
4. Facebook clone

i encounter the error while i try running my server ,thats after creatingvl the polls app ,what could be wrong ?

Re: Learn How To Create Websites With Python(Django Web Framework) by thug: 7:21am On Mar 31, 2020
Mentoli:


i encounter the error while i try running my server ,thats after creatingvl the polls app ,what could be wrong ?

Screenshot your project and app structure, also screenshot the models file that u created, from there we can start our debugging
Re: Learn How To Create Websites With Python(Django Web Framework) by shintaku(m): 7:30am On Mar 31, 2020
Mentoli:


i encounter the error while i try running my server ,thats after creatingvl the polls app ,what could be wrong ?

What are project_2, project_two and aboutme?

It seems you are following some other tutorials by using different project/app names.

So, it will be difficult to tell where you error is related to this poll app tutorial.
Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 11:03am On Mar 31, 2020
thug:


Screenshot your project and app structure, also screenshot the models file that u created, from there we can start our debugging
Re: Learn How To Create Websites With Python(Django Web Framework) by MichelleObama: 6:25pm On Mar 31, 2020
(venv) C:\Users\Owner\PycharmProjects\mysite\mysite\mysite>py manage.py runserver
C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\python.exe: can't open file '
manage.py': [Errno 2] No such file or directory

(venv) C:\Users\Owner\PycharmProjects\mysite\mysite\mysite>


any help concerning this
Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:27pm On Mar 31, 2020
Check Line 2. It should be models.Model not model.Model
Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:32pm On Mar 31, 2020
MichelleObama:
(venv) C:\Users\Owner\PycharmProjects\mysite\mysite\mysite>py manage.py runserver
C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\python.exe: can't open file '
manage.py': [Errno 2] No such file or directory

(venv) C:\Users\Owner\PycharmProjects\mysite\mysite\mysite>


any help concerning this

You need to go back one directory by typing:
cd..(yeah with the 2 fullstop)
Then type py manage.py runserver
Re: Learn How To Create Websites With Python(Django Web Framework) by MichelleObama: 11:19pm On Mar 31, 2020
ColeDrain:


You need to go back one directory by typing:
cd..(yeah with the 2 fullstop)
Then type py manage.py runserver

It works , more grace and blessings, let me go and sleep now that i am on the same page with you

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by castrosteve: 1:05pm On Apr 01, 2020
Good job bro .. I don't really know why am stilstill having error in creating app..and it seems easy Ooo that's y it's annoying ... It's telling me "include "is not defined in polls or so
Re: Learn How To Create Websites With Python(Django Web Framework) by Daejoyoung: 2:17pm On Apr 01, 2020
ColeDrain:


Hmm, you make a point, and WhatsApp may be a bit stressful esp using mobile(for me to type codes...)

I will stick to nairaland guys
Better
Re: Learn How To Create Websites With Python(Django Web Framework) by damtan(m): 2:26pm On Apr 01, 2020
ColeDrain:
Okay let me give an introduction to Django.

Django is a free and open source web application framework, written in Python.

A web framework helps you to develop websites faster and easier. Frameworks exist to save you from having to reinvent the wheel. So instead of spending weeks on creating a website, Django reduces it to days..

Django is:

1.Ridiculously fast.

2.Reassuringly secure.

3.Exceedingly scalable(Can grow and handle increasing load)
I have anaconda installed on my systrm,. Can any of the interface work??

(1) (2) (3) (4) (Reply)

Im A Java Expert Here To Teach Those Who Wants To Learn. / . / Free C++ Reliable UDP Networking Library

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 54
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.