Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,522,267 members, 5,768,142 topics. Date: Monday, 03 August 2020 at 10:39 PM

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

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

Let's Build A Simple Blog With Python(django) / Please Review My Python (django) Site / please review my small python(django) web app (2) (3) (4)

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

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 2:49pm On Apr 01
damtan:

I have anaconda installed on my systrm,. Can any of the interface work??

Yeah. Just install pip to follow the tutorial.
conda install pip
Re: Learn How To Create Websites With Python(Django Web Framework) by damtan(m): 4:22pm On Apr 01
ColeDrain:


Yeah. Just install pip to follow the tutorial.
conda install pip
How do I get that done please. It seems pip is preinstalled. Calling it out is the problems
Re: Learn How To Create Websites With Python(Django Web Framework) by Daejoyoung: 4:58pm On Apr 01
damtan:

How do I get that done please. It seems pip is preinstalled. Calling it out is the problems
Have you typed in pip install django on your command prompt? what was the result? if you don't get anything from that, then try:
conda install -c anaconda django let's see if it works.

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by castrosteve: 10:06pm On Apr 01
pheww finally am doing well... boss Coledrain fire on.... you are doing well sir

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by damtan(m): 10:10pm On Apr 01
Daejoyoung:

Have you typed in pip install django on your command prompt? what was the result? if you don't get anything from that, then try:
conda install -c anaconda django let's see if it works.
I don't think django is preinstalled. Result says " no. matching distribution found for django
Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 10:34pm On Apr 01
damtan:
I don't think django is preinstalled. Result says " no. matching distribution found for django

Please post a screenshot of your anaconda prompt.
Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 11:38pm On Apr 01
POST 13

Things we've learned so far: views, urlpatterns, models, how the web works, admin site

Moving back to the views
We said views are functions that take request from the user(browser) and then generates a response
which is sent to the user's browser. For now this is how our views.py looks like except you added any view to it for practice:

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def index(request):
return HttpResponse("Hello World. You are doing well"wink


Our polls website will have the following views:
index view - displays latest questions to users
detail view - displays details about a specific question
results view - displays results for a particular question
vote view - enable user to vote for a specific choice

So basically, once a user comes to our website's home page, he sees latest questions, he can click on a specific
question and vote for a choice and also see the total results.
Update[b] polls/views.py[/b] as follows:

from django.http import HttpResponse

def index(request):
return HttpResponse("Hello, world. You're at the polls index."wink

def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)

def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)


please convert the emoji to )

Let me explain some things
So we said views are functions that take request from browser.
but some of the views above take another argument question_id
Okay, let's say the user sends a request to django that he wants question 2, how does django
know the question the user wants to view??
Answer- through question_id(so that's why we have question_id)

So user comes to our homepage sees 4 questions, he clicks on number 2, (request, 2) is sent to
django, to help django know the user wants question 2 and then the detail view returns a response
You're looking at question 2.


Like we normally do, after creating our views we create urlpatterns, so go to polls/urls.py and update as such:
(remove the once you created before, if you did)

from django.urls import path

from . import views

urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]


Don't get worried, I will explain. So when a user makes a request he is basically sending a url to django.
You type in nairaland.com/login, you are requesting for nairaland's login page.

So in our polls website, once the user comes to our homepage 127.0.0.1:8000/polls
django will check the mysite/url.py first and if you check your mysite.url/py which is this:

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

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


Django is looking for a urlpattern that matches 127.0.0.1:8000/polls/
once django sees that line[b] path('polls/', include('polls.urls'))[/b] it sees a match then
it goes to the polls/urls.py and then matches path('', views.index, name='index'). You get??

Okay another example to clear things up. A user wants to see question 4, so he clicks it,
this sends 127.0.0.1:8000/polls/4/ to Django, Django starts looking for a match.
It first goes to mysite/urls.py and matches polls and then goes to polls/urls.py and
matches path('<int:question_id>/', views.detail, name='detail')
the <int:question_id>/ matches 4, then Django sends it to the view which then gives a response.

Save the files, then run the server(py manage.py runsever)
Go to your browser and try the following urls:
127.0.0.1:8000.polls/
127.0.0.1:8000.polls/5/
127.0.0.1:8000.polls/5/results/
127.0.0.1:8000.polls/vote/


Always remember user sends a request, a url is sent to the Django, the url contains info
about what the user wants, Django tries to find a match with the urlpatterns it already has,
once a match is found the request is sent to the appropriate view(function) which returns a response to the user.

2 Likes

Re: Learn How To Create Websites With Python(Django Web Framework) by Donoppy: 5:11pm On Apr 02
Nice work @coleDrain, I am currently halfway learning python programming on Udemy and just started reading your content for the first time and they really shed more light to the aspect of Django which am currently on.
ColeDrain:
POST 13

Things we've learned so far: views, urlpatterns, models, how the web works, admin site

Moving back to the views
We said views are functions that take request from the user(browser) and then generates a response
which is sent to the user's browser. For now this is our views.py looks like except you added any view to it for practice:

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def index(request):
return HttpResponse("Hello World. You are doing well"wink


Our polls website will have the following views:
index view - displays latest questions to users
detail view - displays details about a specific question
results view - displays results for a particular question
vote view - enable user to vote for a specific choice

So basically, once a user comes to our website's home page, he sees latest questions, he can click on a specific
question and vote for a choice and also see the total results.
Update[b] polls/views.py[/b] as follows:

from django.http import HttpResponse

def index(request):
return HttpResponse("Hello, world. You're at the polls index."wink

def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)

def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)


please convert the emoji to )

Let me explain some things
So we said views are functions that take request from browser.
but some of the views above take another argument question_id
Okay, let's say the user sends a request to django that he wants question 2, how does django
know the question the user wants to view??
Answer- through question_id(so that's why we have question_id)

So user comes to our homepage sees 4 questions, he clicks on number 2, (request, 2) is sent to
django, to help django know the user wants question 2 and then the detail view returns a response
You're looking at question 2.


Like we normally do, after creating our views we create urlpatterns, so go to polls/urls.py and update as such:
(remove the once you created before, if you did)

from django.urls import path

from . import views

urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]


Don't get worried, I will explain. So when a user makes a request he is basically sending a url to django.
You type in nairaland.com/login, you are requesting for nairaland's login page.

So in our polls website, once the user comes to our homepage 127.0.0.1:8000/polls
django will check the mysite/url.py first and if you check your mysite.url/py which is this:

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

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


Django is looking for a urlpattern that matches 127.0.0.1:8000/polls/
once django sees that line[b] path('polls/', include('polls.urls'))[/b] it sees a match then
it goes to the polls/urls.py and then matches path('', views.index, name='index'). You get??

Okay another example to clear things up. A user wants to see question 4, so he clicks it,
this sends 127.0.0.1:8000/polls/4/ to Django, Django starts looking for a match.
It first goes to mysite/urls.py and matches polls and then goes to polls/urls.py and
matches path('<int:question_id>/', views.detail, name='detail')
the <int:question_id>/ matches 4, then Django sends it to the view which then gives a response.

Save the files, then run the server(py manage.py runsever)
Go to your browser and try the following urls:
127.0.0.1:8000.polls/
127.0.0.1:8000.polls/5/
127.0.0.1:8000.polls/5/results/
127.0.0.1:8000.polls/vote/


Always remember user sends a request, a url is sent to the Django, the url contains info
about what the user wants, Django tries to find a match with the urlpatterns it already has,
once a match is found the request is sent to the appropriate view(function) which returns a response to the user.
..
Nice one bro ... Am following... God bless you .

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by castrosteve: 11:08am On Apr 04
ColeDrain:
POST 13

Things we've learned so far: views, urlpatterns, models, how the web works, admin site

Moving back to the views
We said views are functions that take request from the user(browser) and then generates a response
which is sent to the user's browser. For now this is how our views.py looks like except you added any view to it for practice:

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def index(request):
return HttpResponse("Hello World. You are doing well"wink


Our polls website will have the following views:
index view - displays latest questions to users
detail view - displays details about a specific question
results view - displays results for a particular question
vote view - enable user to vote for a specific choice

So basically, once a user comes to our website's home page, he sees latest questions, he can click on a specific
question and vote for a choice and also see the total results.
Update[b] polls/views.py[/b] as follows:

from django.http import HttpResponse

def index(request):
return HttpResponse("Hello, world. You're at the polls index."wink

def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)

def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)


please convert the emoji to )

Let me explain some things
So we said views are functions that take request from browser.
but some of the views above take another argument question_id
Okay, let's say the user sends a request to django that he wants question 2, how does django
know the question the user wants to view??
Answer- through question_id(so that's why we have question_id)

So user comes to our homepage sees 4 questions, he clicks on number 2, (request, 2) is sent to
django, to help django know the user wants question 2 and then the detail view returns a response
You're looking at question 2.


Like we normally do, after creating our views we create urlpatterns, so go to polls/urls.py and update as such:
(remove the once you created before, if you did)

from django.urls import path

from . import views

urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]


Don't get worried, I will explain. So when a user makes a request he is basically sending a url to django.
You type in nairaland.com/login, you are requesting for nairaland's login page.

So in our polls website, once the user comes to our homepage 127.0.0.1:8000/polls
django will check the mysite/url.py first and if you check your mysite.url/py which is this:

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

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


Django is looking for a urlpattern that matches 127.0.0.1:8000/polls/
once django sees that line[b] path('polls/', include('polls.urls'))[/b] it sees a match then
it goes to the polls/urls.py and then matches path('', views.index, name='index'). You get??

Okay another example to clear things up. A user wants to see question 4, so he clicks it,
this sends 127.0.0.1:8000/polls/4/ to Django, Django starts looking for a match.
It first goes to mysite/urls.py and matches polls and then goes to polls/urls.py and
matches path('<int:question_id>/', views.detail, name='detail')
the <int:question_id>/ matches 4, then Django sends it to the view which then gives a response.

Save the files, then run the server(py manage.py runsever)
Go to your browser and try the following urls:
127.0.0.1:8000.polls/
127.0.0.1:8000.polls/5/
127.0.0.1:8000.polls/5/results/
127.0.0.1:8000.polls/vote/


Always remember user sends a request, a url is sent to the Django, the url contains info
about what the user wants, Django tries to find a match with the urlpatterns it already has,
once a match is found the request is sent to the appropriate view(function) which returns a response to the user.

Something is not right,,,I keep getting syntax error...I copied and pasteed the codes in views.py and url.py..,I also changed the emoji to close bracket..j still don't y am seeing syntax error
Re: Learn How To Create Websites With Python(Django Web Framework) by Daejoyoung: 11:44am On Apr 04
castrosteve:


Something is not right,,,I keep getting syntax error...I copied and pasteed the codes in views.py and url.py..,I also changed the emoji to close bracket..j still don't y am seeing syntax error
lndentation matters in python as well, so while copying and pasting codes, check your indentation.
Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 12:20pm On Apr 04
castrosteve:


Something is not right,,,I keep getting syntax error...I copied and pasteed the codes in views.py and url.py..,I also changed the emoji to close bracket..j still don't y am seeing syntax error

Help us to help you. Post screenshots of the error.
Re: Learn How To Create Websites With Python(Django Web Framework) by castrosteve: 6:42pm On Apr 04
ColeDrain:


Help us to help you. Post screenshots of the error.

ok

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 10:37pm On Apr 04
castrosteve:


ok

Check line 8 of your polls/views.py. Hope it's like this:
def detail(request, question_id):

If it is like that, then screenshot your views.py file.
Re: Learn How To Create Websites With Python(Django Web Framework) by castrosteve: 9:20am On Apr 05
ColeDrain:


Check line 8 of your polls/views.py. Hope it's like this:
def detail(request, question_id):

If it is like that, then screenshot your views.py file.

its def function
pass detail

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 10:23am On Apr 05
castrosteve:


its def function
pass detail

It should be this instead:
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
Re: Learn How To Create Websites With Python(Django Web Framework) by MichelleObama: 1:47pm On Apr 05
I am having the same sintax error as well
Re: Learn How To Create Websites With Python(Django Web Framework) by FredLum: 9:46pm On Apr 05
A suggestion, I implemented this:


urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]


And the url was http://127.0.0.1:8000/2/results/ and http://127.0.0.1:8000/2/vote/

I modifed to this:

path('<int:question_id>/', views.detail, name='detail'),
path('results/<int:question_id>/', views.results, name='results'),
path('vote/<int:question_id>/', views.vote, name='vote'),

so the url http://127.0.0.1:8000/results/2/ or http://127.0.0.1:8000/vote/2/ worked, which I think is more preferable following OOP standards
Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 11:35pm On Apr 05
FredLum:
A suggestion, I implemented this:


urlpatterns = [
path('', views.index, name='index'),
path('<int:question_id>/', views.detail, name='detail'),
path('<int:question_id>/results/', views.results, name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]


And the url was http://127.0.0.1:8000/2/results/ and http://127.0.0.1:8000/2/vote/

I modifed to this:

path('<int:question_id>/', views.detail, name='detail'),
path('results/<int:question_id>/', views.results, name='results'),
path('vote/<int:question_id>/', views.vote, name='vote'),

so the url http://127.0.0.1:8000/results/2/ or http://127.0.0.1:8000/vote/2/ worked, which I think is more preferable following OOP standards

Glad you are following to the point of even tweaking it to your preference. But:

First, OOP has nothing to do with url patterns basically.
Second, we first visit a question before we can then vote or check result that's why int:question_id comes first.
Third, any of them is okay as far your URLs are intuitive to the user.

Hope you get??
Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 11:36pm On Apr 05
Sorry guys, I have been busy. Will continue tomorrow. Hope you are all doing well?
Re: Learn How To Create Websites With Python(Django Web Framework) by castrosteve: 11:51pm On Apr 05
ColeDrain:


It should be this instead:
def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)

ok it works but am getting 404 error but the url works with /admin

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 1:30am On Apr 06
castrosteve:

ok it works but am getting 404 error but the url works with /admin

Okay screenshot your polls/urls.py file and mysite/urls.py file.
Re: Learn How To Create Websites With Python(Django Web Framework) by castrosteve: 9:58am On Apr 06
ColeDrain:


Okay screenshot your polls/urls.py file and mysite/urls.py file.

ok

Re: Learn How To Create Websites With Python(Django Web Framework) by castrosteve: 10:04am On Apr 06
mysite/url.py

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 12:34pm On Apr 06
castrosteve:
mysite/url.py

Okay the problem is from your mysite/urls.py file.

Change this line:
path('', include('polls.urls')),
to
path('polls/', include('polls.urls')),

And try again from ur browser.
Re: Learn How To Create Websites With Python(Django Web Framework) by castrosteve: 1:34pm On Apr 06
ColeDrain:


Okay the problem is from your mysite/urls.py file.

Change this line:
path('', include('polls.urls')),
to
path('polls/', include('polls.urls')),

And try again from ur browser.

phew... am all good thankss...

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by FredLum: 3:20pm On Apr 06
ColeDrain:


Glad you are following to the point of even tweaking it to your preference. But:

First, OOP has nothing to do with url patterns basically.
Second, we first visit a question before we can then vote or check result that's why int:question_id comes first.
Third, any of them is okay as far your URLs are intuitive to the user.

Hope you get??

No p, meant URL pattern standard not OOP standard.

When will you drop the next lecture?

Regards

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by damtan(m): 6:34am On Apr 09
sorry bro. been off for a while. here is the screenshot
ColeDrain:


Please post a screenshot of your anaconda prompt.

Re: Learn How To Create Websites With Python(Django Web Framework) by Daejoyoung: 9:01am On Apr 09
damtan:
sorry bro. been off for a while. here is the screenshot
Go to Anaconda prompt in your system, then type your codes there to see what gives.
In Anaconda prompt, type....conda install pip.
To get to Anaconda prompt, just type Anaconda prompt in the search area of your computer. OR

just use conda only, start with:

conda install -c anaconda django in your anaconda prompt not jupyter notebook.

1 Like

Re: Learn How To Create Websites With Python(Django Web Framework) by damtan(m): 9:56am On Apr 09
Daejoyoung:

Go to Anaconda prompt in your system, then type your codes there to see what gives.
In Anaconda prompt, type....conda install pip.
To get to Anaconda prompt, just type Anaconda prompt in the search area of your computer. OR

just use conda only, start with:

conda install -c anaconda django in your anaconda prompt not jupyter notebook.
I have done all these before this morning in the anaconda prompt and I have successfully created, activated and installed djangoenv in anaconda navigator.
I have even seen the mysite folder and all other parameters that you stated created on my system.
The problem is proceeding with the last command which is
cd mysite
py manage.py runserver
An error message keeps popping

Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 3:55pm On Apr 09
damtan:

I have done all these before this morning in the anaconda prompt and I have successfully created, activated and installed djangoenv in anaconda navigator.
I have even seen the mysite folder and all other parameters that you stated created on my system.
The problem is proceeding with the last command which is
cd mysite
py manage.py runserver
An error message keeps popping

I don't really get your problem.
But in your Anaconda prompt not jupyter notebook go to the directory that contains the manage.py file and type: py manage.py runserver

Screenshot your anaconda prompt with the error there.
Re: Learn How To Create Websites With Python(Django Web Framework) by damtan(m): 6:19pm On Apr 09
ColeDrain:


I don't really get your problem.
But in your Anaconda prompt not jupyter notebook go to the directory that contains the manage.py file and type: py manage.py runserver

Screenshot your anaconda prompt with the error there.
Alright
I'll do that soon
Re: Learn How To Create Websites With Python(Django Web Framework) by KlausMichaelson: 8:51am On Aug 02
And that was how the story ended?? sad

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

Knickknacks For Programmers/ Web Developers And UX/UI Professionals / Native JAVA Android Developers - Let Us Discuss Techie Stuffs / Programming in C++: A tutorial

(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 - 2020 Oluwaseun Osewa. All rights reserved. See How To Advertise. 290
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.