Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,427 members, 7,954,670 topics. Date: Saturday, 21 September 2024 at 06:29 AM |
Nairaland Forum / ColeDrain's Profile / ColeDrain's Posts
(1) (2) (3) (4) (5) (6) (7) (of 7 pages)
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 10:37pm On Apr 04, 2020 |
castrosteve: 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. |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 12:20pm On Apr 04, 2020 |
castrosteve: Help us to help you. Post screenshots of the error. |
Crime / Re: Italian Nurse Strangles Doctor Girlfriend. Says She Infeced Him With Coronavirus by ColeDrain(m): 9:36am On Apr 02, 2020 |
Coronavirus is not a death sentence, people are recovering from it. Did killing her cure him?? Some people are alive but dead. |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 11:38pm On Apr 01, 2020 |
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 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 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 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 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 |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 10:34pm On Apr 01, 2020 |
damtan: Please post a screenshot of your anaconda prompt. |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 2:49pm On Apr 01, 2020 |
damtan: Yeah. Just install pip to follow the tutorial. conda install pip |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:32pm On Mar 31, 2020 |
MichelleObama: You need to go back one directory by typing: cd..(yeah with the 2 fullstop) Then type py manage.py runserver |
Programming / 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 |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 11:03am On Mar 31, 2020 |
thug: |
Programming / 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 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
|
Programming / 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? 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 , 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 |
Programming / 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 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 |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 10:30am On Mar 30, 2020 |
Abcruz: 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 |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:59am On Mar 30, 2020 |
Tolzy11: Okay I will do that now P:S For some reasons I won't create a group now, maybe in the future |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:38pm On Mar 29, 2020 |
MichelleObama: Yeah correct, but note urls.py(in small letters) |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:37pm On Mar 29, 2020 |
thug: - 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.. |
Programming / 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): 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 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 |
Programming / 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 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 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
|
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 2:31pm On Mar 28, 2020 |
MichelleObama: Like your determination. Out of power too, will get back as soon as I can. |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 12:38pm On Mar 28, 2020 |
thug: Have you created a virtual environment and is it activated? |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 12:36pm On Mar 28, 2020 |
MichelleObama: Have you solved the problem yet? |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 12:35pm On Mar 28, 2020 |
nelxxy: Thanks for the help, your assistance is highly needed. |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 8:53am On Mar 28, 2020 |
POST 8 Back to course, our server is still running from the command prompt, to stop it type ctrl + c. If you closed your CMD prompt(make sure you activate your virtual environment and go to the directory of mysite as shown below) (env) C:\Users\AYOMIDE\mysite> Now in POST 6 we created a project file structure, now let's create an app file structure(I've explained the diff btw app and project) Run this in your CMD: py manage.py startapp polls That will create an app named 'polls' basically a folder on your computer with various files in them as shown below(open your file explorer you should see it) polls/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py Okay now you need to open your text editors(notepad++, sublime text, vs code, atom etc), I will advise to download sublime text, abeg no use notepad. Open up the mysite folder in your sublime text, then open the views.py file in the polls directory and type in the following python code. from django.shortcuts import render pls put a bracket inplace of that emoji(nairaland ish).. You should have something like the picture below If you are lost tell me 2 Likes
|
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 8:17am On Mar 28, 2020 |
POST 7 HOW THE WEB WORKS as I promised You are browsing with your phone, you want to go to Nairaland website, you type in nairaland.com, you press enter- immediately you do this you are making a request to a web server(it holds all the code of a website) asking for nairaland home page (nairaland is just a bunch of code, basically). The server takes the url(nairaland.com) and tries to match it with a set of urlpatterns(it's just trying to know what the user wants) Once it finds a match, it passes the request to a function(which is called view in django), the view then generates a response which is sent to the user's browser. To make it more simplified- Imagine a postman with letters(take the letters as urls), he checks the address of each house to see if it matches with what he has, once he finds a match he drops the letter there. You get?? I hope you have an idea of how the web works.. 2 Likes
|
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 10:53pm On Mar 27, 2020 |
Okay I have to go and rest. We will def continue tomorrow. I just want to know that I am not alone, drop your feedback, questions and problems. I am here for you. Good night 1 Like |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 10:39pm On Mar 27, 2020 |
POST 6 Let's create an initial project file structure Run the following in your command prompt: django-admin startproject mysite This will create a Django project with the name mysite. If you go into your file explorer you would see a directory called mysite which will contain the following: mysite/ - outer directory manage.py - allows us to interact with our project in many ways mysite/ - inner directory __init__.py - An empty file that tells Python to treat the mysite directory as a Python module. asgi.py settings.py - as the name implies urls.py - holds your URL patterns wsgi.py You know I said Django helps save time, so Django comes with all these originally, we don't need to start recreating them again. So don't delete anything. Now let's check if what we have done so far works.. Run the following in your command prompt: cd mysite py manage.py runserver Django comes with a lightweight server, so you don't need to worry about setting up one until you want to go live(internet). Go to your browser and visit http://127.0.0.1:8000/ You'll see a congratulations page, something like the image below. Phew.. Hard work, things are about to get more interesting.. 1 Like
|
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:46pm On Mar 27, 2020 |
POST 5 We are going to create our first project. What does project mean? In simple terms take a website like Facebook it's a project made up of many functionalities which in Django we call app. Basically the functionalities are called apps, while the entirety is the project. You get?? Our first project will be a polling website- where users vote for a preferred choice like they do on Twitter or FB. Something like the image below.. 1 Like
|
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:33pm On Mar 27, 2020 |
POST 4 We have installed Python and created a virtual environment, moving on.. Let us install Django In your command prompt type in: pip install Django This will install Django in your virtual environment.. N:B If you closed your command prompt you need to activate your virtual environment again using env\Scripts\activate |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:27pm On Mar 27, 2020 |
N:B Hey people. If you have any questions or you encounter any error feel free to ask me.. 1 Like |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 9:18pm On Mar 27, 2020 |
POST 3 Now python is installed on your device. Since a person may have different web projects on his system, it is advisable to have a version control mechanism, this helps to exclude a project to its packages, therefore preventing other projects from clashing, so its like each project is independent from others... Python calls this virtualenv meaning virtual environment. In your command prompt type pip install virtualenv make sure you are connected to the internet. This will install the virtualenv package. Now let's create an isolated environment using the virtualenv we have installed. In your command prompt type in virtualenv env, this will create a env directory. After creating your virtual environment, we need to activate it, in your command prompt type in env\Scripts\activate Kudos.You have successfully created a virtual environment. You will get to understand the need as we advance. 1 Like |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 8:57pm On Mar 27, 2020 |
POST 2 Make sure you have installed on your system the latest Python version to avoid any compatibility issue. To check whether you have installed Python correctly open up your Command Prompt and type in py --version This should return the python version you have installed.. Meaning python installed successfully. 1 Like |
Programming / Re: Learn How To Create Websites With Python(Django Web Framework) by ColeDrain(m): 7:41pm On Mar 27, 2020 |
At least eight more people needed.. Two people have already shown interest. |
(1) (2) (3) (4) (5) (6) (7) (of 7 pages)
(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. 80 |