Creating A New Django Blog (Setup + Introduction) | Python Django
We have come so far in our Django journey. Till now, we've successfully created two projects, first is the textutils website, and another is our E-commerce website. Now, it's time to start our third Django project. We will be creating a blog named "Icoder" with the help of Django and the Bootstrap. In the previous tutorials, I've already covered all the basics of Django. While creating the Icoder blog, we will look at some advanced Django concepts such as authentication, authorization etc. I will use the Visual Studio IDE; feel free to use any other IDE of your choice. So, without wasting time, let's start the setup of the Icoder blog.
Step 4: Now, open a terminal in the VS code and type the below code to start the project:
django-admin startproject iCoder
Step 5: Now, we will create a blog app in the iCoder project. Get into the directory containing the manage.py file and type the below code in the VS code terminal to create the app :
python manage.py startapp blog
This is all for this tutorial, and I hope that you are enjoying this Django journey. You can ask your doubts in the QnA section.
No Source Code Associated With This Video
Steps To Open The Project In VS Code:
Step 1: Create a directory named "Icoder" in your pc. Step 2: Open the Icoder folder, and do "Shift + Right Click." You will see the following screen :
Step 3: Click on the "Open with code," and the Icoder directory will get open in the VS code.
Django Blog: Project Setup & URLConfs | Python Django Tutorials In Hindi #72
We will start this tutorial by creating a file named 'requirements.txt,' and we will create a virtual environment for our project. So, make a new file called "requirements.txt" in the main icoder directory. After this, install the virtual environment by typing the below command in the existing terminal of the VS code :
pip install virtualenv
After successfully installing the virtual environment, create a new virtual environment by typing the below code :
virtualenv myprojectenv
In the above code, we are creating a new virtual environment named "myprojectenv." After running the above command, you will notice that a new folder named myprojectenv is created inside the main icoder directory. After this, we need to activate the virtual environment that we've just created. Get into the myprojectenv and type the below code to activate the virtual env :
.\Scripts\activate
There is a chance that you encounter the below error after executing the above command:
.\Scripts\activate : The term '.\Scripts\activate'isnot recognized as the name of a cmdlet, function, script file,or operable program. Check the spelling of the
name,orif a path was included, verify that the path is correct andtry again.
At line:1 char:2+.\Scripts\activate
+~~~~~~~~~~~~~~~~~~+ CategoryInfo : ObjectNotFound:(.\Scripts\activate:String)[], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Steps to resolve the above error:
Step 1: Head over to File--> Preferences--> Settings as shown in the below image.
Step 2: After this, search "extensions" and click on "Edit in settings.json."
Step 3: Now, we need to set the command line policies. To do so, type the below code in the settings.json file and VS code will automatically set the command line policies for you :
Restart the VS code and try to activate the virtual environment. If you've followed the above steps correctly, then your virtual environment will get activated. This tutorial ends here, and in the next tutorial, we will how to properly create and manage apps in Django.
No Source Code Associated With This Video
Django Blog: Creating Django Apps & Properly Managing Them | Python Django Tutorials In Hindi #73
In this tutorial, we will create apps for the iCoder blog. We will start by creating a new app named home. Endpoints like about, contact page, etc. will be contained inside this app. Type the below code to make the home app:
python manage.py startapp home
After this, open the views.py file of the icoder directory and type the below code :
"""iCoder URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""from django.contrib import admin
from django.urls import path, include
urlpatterns =[
path('admin/', admin.site.urls),
path('', include('home.urls')),
path('blog/', include('blog.urls')),]
In the above code, we've created two URLs, first is for home, and the second one is for the blog app. So, whenever the user tries to access the http://127.0.0.1:8000/ then, the control will be redirected to the urls.py file of the shop app. Similarly, whenever the user attempts to access the http://127.0.0.1:8000/blog/, then the control will be sent to the urls.py file of the blog app. Now, we will map the views and URLs of the home app. Open the urls.py file of the home and type the below code:
from django.contrib import admin
from django.urls import path, include
from home import views
urlpatterns =[
path('', views.home, name="home"),
path('contact', views.contact, name="contact"),
path('about', views.about, name="about"),
Now, open the views.py file of the home app and type the code given below code:
from django.shortcuts import render, HttpResponse
# Create your views here.defhome(request):return HttpResponse('This is home ')defcontact(request):return HttpResponse("This is contact")defabout(request):return HttpResponse('This is about')
Restart the development server and check whether all the endpoints for the home app are working correctly or not. Let's start mapping URLs and views for the blog app. Open the urls.py file of the blog app and type the below code:
from django.contrib import admin
from django.urls import path, include
from.import views
urlpatterns =[
path('', views.blogHome, name="bloghome"),
path('<str:slug>', views.blogPost, name="blogPost"),]
In the above code, we've created a slug field for the blogPost function. Let's spend some time discussing the slug filed in Django.
Slug Field in Django :
As we are creating a blog, so it is clear that our will contain more than one blog post. So, how can we display the requested blog post to the user? Let's assume we have a blog post with the title "Django Made Easy By Code With Harry" with a unique id=4. One way to refer to this blog post is to append the id at the end of the URL like http://127.0.0.1:8000/blog/4, or we can use http://127.0.0.1:8000/blog/Django Made Easy By Code With Harry.The second URL format looks more detailed, and here comes the concept of the slug field in Django. With the help of the slug field, we can convert the title into the slug. Now, open the views.py file of the shop app and type the below code:
from django.shortcuts import render, HttpResponse
# Create your views here.defblogHome(request):return HttpResponse('This is blog home. We will keep all blog posts here')defblogPost(request, slug):return HttpResponse(f'This is blogPost : {slug}')
Restart the development server and head over to the blog and type anything after the "/blog" in the URL. You will notice that the texted after "/blog" is displayed on the screen. You can also find the same in the below image :
This tutorial ends here, and in the upcoming tutorials, we will how to automatically convert the title of the blog posts into the slug. Feel free to ask your queries in the QnA section.
Django, a high-level Python web framework, is well-known for its robustness and versatility. While Django natively supports popular databases like PostgreSQL and MySQL, you may want to integrate a NoSQL database like MongoDB for specific use cases. MongoDB’s flexibility and scalability make it an excellent choice for handling unstructured data. In this blog, we will walk you through the process of connecting MongoDB to a Django project, enabling you to harness the power of NoSQL in your web applications. Prerequisites: Before we begin, ensure you have the following prerequisites in place: Django installed on your system. MongoDB database server installed and running. Basic familiarity with Django project structure and database concepts. Virtual Environment, this is optional but recommended. You check our blog here . Note: For this tutorial, we are using our basic skeleton project for Django. You can also download the project from here . Step 1: Insta...
in this post, we will see how to use nginx with gunicorn to serve django applications in production. Django is a very powerful web framework and ships with a server which is able to facilitate development. This development server is not scalable and is not suited for production. Hence we need to configure gunicorn to get better scalability and nginx can be used as a reverse proxy and as a web server to serve static files. Let's get started Step 1 - Installing python and nginx Let's update the server's package index using the command below: sudo apt update Copy sudo apt install python3-pip python3-dev nginx Copy This will install python, pip and nginx server Step 2 - Creating a python virtual environment sudo pip3 install virtualenv Copy This will install a virtual environment package in python. Let's create a project directory to host our Django application and create a virtual environment inside th...
All MongoDb commands you will ever need (MongoDb Cheatsheet) In this post, we will see a comprehensive list of all the MongoDB commands you will ever need as a MongoDB beginner. This list covers almost all the most used commands in MongoDB. I will assume that you are working inside a collection named 'comments' on a MongoDB database of your choice 1. Database Commands View all databases show dbs Copy Create a new or switch databases use dbName Copy View current Database db Copy Delete Database db . dropDatabase ( ) Copy 2. Collection Commands Show Collections show collections Copy Create a collection named 'comments' db . createCollection ( 'comments' ) Copy Drop a collection named 'comments' db . comments . drop ( ) Copy 3. Row(Document) Commands Show all Rows in a Collection db . comments . find ( ) Copy Show all Rows in a Collection (Prettified) db . comments . find ( ) . pretty ( ) Copy Find the first row matching the ob...
Comments
Post a Comment