Connect with us

django

Create a website with django in [2020]

Published

on

You probably searching about website with django or build a website with django because you know the strength that django provides while developing website. When you think about making a website then some few things that come into your mind is that you need to be an expert coder to build a website or something like that. You need to write a whole bunch of code to do so but believe me it’s not. Here we will discuss building a website in django framework. You probably already know the strength of django but I want to give you a little brief introduction about what a django framework is and which languages you can use to build a website with django.

What is django?

Django is a framework for perfectionists with deadlines. What I mean by this is simple. You can build any kind of website or web application using this framework and the main specialty in this framework is that it uses python as its backend language and you might already know about python’s simple syntax and popularity. So without wasting much time lets dive into building our website.

What you need to make a website with django?

  1. You need to know about python basic syntax
  2. You need to know about html, css.
  3. Some django basics like Urls, models, views etc.

Setting Up Django in our system:

In order to start making a website in django the no. 1 thing that you will require is python. So let’s go ahead and install python.

Then you need to install django using command prompt. You can refer to django documentation but i will explain it below.

You can install virtual environment and Django by clicking here.

A thought about our website with django:

Our website will have a homepage where we will display our short introduction and list of our posts. Then we have a detail page where we will display the detail of the posts whenever a user clicks on any post.

We will have an administration site where we will be able to add post, delete post, or update our posts.

So let’s start working on our website.

In order to create a project in django you need to be inside the folder where you install virtual environment. Activate your virtual environment and type the following command to create a django project.

>>> django-admin startproject mySite

You will see a folder created inside the folder that you are currently in. The next thing you should do is to check that weather your project is successfully created or not. To check this just type

>>> python manage.py runserver

You will see something like this.

Then inside command prompt type the following command to create an app called blog.

>>> python manage.py startapp blog

You will see a new folder created inside the root directory of your project.

Now open you project folder inside a text editor. You will see something like this.

Folder structure django website mubashartech.com

The upper folder will be the root folder where all your project folders, files and databases are stored and the folder named as blog will be your app that you have just created.

Open up settings.py file inside mySite subfolder and register blog application under installed apps. It will look like this.

Now our project is ready to be worked with. Let’s open up models.py file inside blog application folder and type the below lines of code.

from django.db import models
class Article(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    author = models.CharField(max_length=50)
    date = models.DateField()
    def __str__(self):
        return self.title

Now let’s go to command prompt and migrate our database so that our model in synced inside database.

To migrate our database, type the following command inside command prompt.

>>> python manage.py makemigrations

And finally let’s migrate our database

>>> python manage.py migrate

You will see something like this:

migrations mubashartech.com

You may something different as I previously migrated my database but that will be similar to this.

Now our model is synced inside our database. Let’s open up admin.py file inside blog application folder and type the following lines of code to it.

from django.contrib import admin
from .models import Article
admin.site.register(Article)

Now let’s go ahead and create a super user for our website.

To create a super user in django just go to your command prompt and type the following command.

>>> python manage.py createsuperuser

You will be asked to fill out your name, email and password and once you confirmed your password your super user will be created.

Now you can access django provided administration site and since we registered our model in admin.py file you will be able to create articles from there.

Open up command prompt and type the following command to run your project.

>>> python manage.py runserver

This will run your project. Now open up any browser and type the following address there.

127.0.0.1:8000/admin and you will see a page like this.

administration page screenshot mubashartech.com

Just fill in the user name and password you just created and click on login.

Once you logged in then you will see a page like this:

Articles adding page in django admin mubashartech.com

Just click on articles and on the right hand corner of the next screen you will find a button named Add Articles. Once you added some articles then just follow along. I recommend adding up to 2 or 3 dummy articles that we can display into our site. Just add some don’t worry you can change them later on.

Now we will be creating our views.

So open up views.py file inside blog application and add these lines of code.

from django.shortcuts import render
from .models import Article
def homeView(request):
    article = Article.objects.all()
    context = {'articles': article}
    return render(request, 'blog/index.html', context)
def detailView(request, pk):
    article = Article.objects.get(pk=pk)
    context = {'article': article}
    return render(request, 'blog/post.html', context)
def aboutView(request):
    return render(request, 'blog/about.html')

Now I will explain the above code. We are creating 3 views namely homeView, detailView and aboutView.

homeView: So this view will handle the request when a user request for home page. For example, if someone come into the main url of your website then this view will be called.

detailView: We will have a list of our articles in our homepage and once a user clicks on any post list then this view will render detail of that post.

aboutView: This view will render details about you whenever someone want to know about you or whenever someone click on the about page link in the navigation menu.

Now let’s create the templates we are using inside our views, so let’s do that:

Now go ahead and create a new folder inside blog application folder and name it as templates and then create a new folder inside that template folder and name it as blog. Your folder structure might look like this.

template folder mubashartech.com

Create a file inside blog folder you just created and name it as index.html and add these lines of code.

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title> My website</title>
  </head>
  <body>
    {% for article in articles %}
<ul>
       <li><a href="{{article.pk}}">{{article.title}}</a></li>
</ul>
{% endfor %}
  </body>
</html>

Now let’s create another file inside blog folder and add these lines of code to it.

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>{{article.title}}</title>
  </head>
  <body>
    <h1> {{article.title}} </h1>
	<p> {{article.content}} </p>
	<p> by {{article.author}} </p>
	<p> at {{article.date}} </p>
  </body>
</html>

So we have created the home and detail page of our website. Now let’s create the last page inside blog folder and name it as about.html and add these lines of code to it.

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title> about page </title>
  </head>
  <body>
    <h1> Hi here is what I do </h1>
	<p> I am a programmer and teaching people how to code. You can check my other tutorials at mubashartech.com </p>
  </body>
</html>

Last step in creating our website, create a file inside blog application and name it as urls.py and add these lines of code to it.

from django.urls import path
from . import views
urlpatterns = [
    path('', views.homeView, name='home'),
    path('<int:pk>/', views.detailView),
    path('about/', views.aboutView, name='about')
]

Let’s register this urls.py file inside the main urls.py file of our project. Open the urls.py file from mySite folder and add these lines of code to it. Note: You must open the file mySite/urls.py and add these lines.

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('', include('blog.urls')),
    path('admin/', admin.site.urls),
]

Now our project is created successfully and lets test this by typing the following command inside command prompt.

Python manage.py runserver

And open browser and type the following address in the address bar:

127.0.0.1:8000 and you will see your page up and running. Click on any post link and you will see the details of that post. And if you want to open about page then type 127.0.0.1:8000/about and you will see the page that you have created for your users to know about you. That’s all and if you have any question you can ask it in the comment section and I will definitely answer. In the next article I will teach you how to style this website to make it look more cool.

Continue Reading
3 Comments

3 Comments

  1. Pingback: Online Teech

  2. Pingback: Online Teech

  3. Pingback: Online Teech

Leave a Reply

Your email address will not be published. Required fields are marked *

django

How to Connect django to mysql in ubuntu[2020]

Published

on

By

Connect Django to MySQL is required because the database that comes with Django by negligence is sqlite3. Sqlite3 is a database that can be used inside small apps and outlines. But when it comes to exhibiting some high-quality apps/web apps in Django, then it became wanted to connect it with a good database that can handle a large amount of data. In this post, we are working to do that.

To rise connecting our Django project to the database, you should oblige python, Django and XMPP server installed on your system.

To install xampp on your system, you require to download the app and next install it inside your system. Here is an expeditious guide to installing xampp on your ubuntu running system.

You can likewise install the virtual environment and Django by following this example.

If you have Django and xampp installed on your system, suddenly you can start to connect Django to MySQL.

Create a new project in Django:

To build a new project in Django, just type the following commands in your terminal.

Django-admin start project MySQL connection

Open up xampp and run everything the services.

Once you do that, just go to localhost/PHPMyAdmin and generate a new database.

I will name it as MySQL connection, and you can name it whatever you need.

Once you function to create, then you are good to go. You don’t need to change anything there.

Startup settings.py file inside your Django project and move to the following part.

By default, our Django project is pointing to the sqlite3 database, and we require to improve this in order to manage a MySQL database. So let us go ahead and replace all of this text to the following code.

Once you change these settings, then you are good to go.

Now Save all the settings and open up the terminal and type the following command.

python manage.py migrate

Everything is running fine, and if you go to PHPMyAdmin, then you would reasonably see something like this.

You see that all the tables are favourably created. Thanks for reading this comprehensive post. If you have any question, you can ask me in the comment section. Thanks.

Continue Reading

django

5 amazing books to read as a django developer [2020]

Published

on

By

Hi, in this chapter, I will talk about five superb books to learn as a Django developer in 2020. So let us perceive started.

As you all might now that books play a significant role in learning anything weather, it is about the the stock market or any other field like programming. So as readers have a significant role in learning to program quickly and efficiently, we will be discussing five amazi////////ng books to read as a Django developer in 2020 because Django is a high-level framework. If you want to master it, then you should probably go with the books written by Django experts to learn the important topics of Django.

My no. 1 advice for Django beginners:

Before diving into the books list first let us discuss what should a beginner do when it was just starting Django development.

When You are starting in Django, then you should go with its Documentation.

Because Django has very comprehensive and clear Documentation and you could see a lot of Documentation. But that Documentation is written professionally, and as a beginner, you might be stuck. So here is the list of books you might concern when you are starting in Django development.

Learn through building projects in Django:

While you are learning programming, you might not follow some professional practices that will lead you to be good at it. So to understand the important stuff that you will need to use in some professional projects, you should start to learn to code by building projects.

I have some cool projects that I write about so that beginners in Django and intermediate level programmers get the benefit. Here is the list of some cool projects that I recommend you to start building with.

i – Create your website with Django: you can get the tutorial by clicking here.

ii – Create a resume builder website with Django: Here is the complete tutorial that you can follow.

iii – How to use bootstrap with Django: This post will teach you the best way to use bootstrap with your Django projects to make them responsive. Get it here.

Also,, I have prepared a list of projects that you can build right now. You can check that complete article here.

Now you can begin with the list of amazing books for you to be good at Django.

A list of 5 Amazing books you should read as a Django developer.

Book # 1:

Django for Beginners (best book in this list):

The best book on Django is Django for beginners by William S. Vincent. The reason why I kept this book on top of the list is that it is extremely summarized and also very comprehensively explained all the topics for beginners. This books starts from beginner teaching the basics of Django and then moves you around some high-level programming in Django framework whenever you feel comfortable in Django development. In short, it will teach everything you need to become a master in the Django framework.

Book # 2:

Django 2 web development Cook Book:

The next great book in this list is also a very great book for beginners. It is written by Jake Kronika, and it also covers a lot of beginners stuff. This book talks about Django 2, but the logic builds through this book can be used inside Django 3.

Book # 3:

Django 2 by Example:

This book is written by Antonio Mele, and it is one of my favourite books because it starts with build the projects and then by the end of building many projects taught in this you will eventually become a master in Django.

Book # 4:

Two Scoops of Django:

Another favourite book of mine is Two scoops of Django, and it is a bit older, it only talks about Django 1.8 but the logic you will learn in this book can be implied in the latest versions.

Book # 5:

The definitive guide to Django:

This book is also very informative if you want to learn Django web development from start to end. Once you finish reading and practising the lessons inside this book, then you will have a thorough understanding of how Django works.

I hope you have enjoyed a lot reading this tutorial now if you have any questions feel free to ask it in the comment section and I will definitely answer. You can also check out the post about Django books by data-flair here. Thanks.

Continue Reading

django

Amazing Project ideas for beginners in django [2020]

Published

on

By

django

Hi in this article i will introduce you some of my Amazing project ideas for beginners in django, So lets get Started

Remember one thing in mind you cannot proceed further without making projects and practicing much in the language or framework you are working right now because once you make some projects, eventually your knowledge will become clear and you will start doing what you want to do in programming.

Now you might know the importance of making projects in django. Lets Discuss some Amazing project ideas for beginners in django.

No 1: Build a TodoList App

While building this project you will learn quite a lot in django because it will give you in-depth knowledge of how everything fits up with each other in django because you will be using html, css and javascript with django for making your todolist app cool and you will learn a lot.

No 2: Build Your own Blog Using django:

The importance of building your own blog for being a programming is even more. Because by having a blog you can share your thoughts with other, and you will learn a lot by doing so. If you want to make a project using django then you should go with making your own blog.

No 3: Build a Personal website using django:

This is quite similar to the application above because you can embed your blog inside a static home page website as well. So here is my advice for creating your website.

Try to create a website with static homepage and create contact and about us pages. Then create a blog application and embed it inside your personal website. In this your website will look more professional and you will learn a lot of fundamentals of django by building this project as well. So go ahead and do that. I have a pretty great tutorial on this project and you can reference that tutorial as by going to the below link.

my personal website tutorial link: click here.

No 4: Build your own portfolio Website:

Let me introduce you what portfolio website really is:

A portfolio website is simply a website where you can show case your work. Try to make a website where you will be able to tell other people what you can do and post some of your projects their.

I have a comprehensive tutorial on this project as well, you can check that as well.

No 5: Build an Ecommerce Website:

Building an ecommerce website in django can be challenging and it will also helps you to make a strong logic about how to build an ecommerce website using django. Try to make a simple online shop and add cart options to it and then you can use this application to sell your projects online.

You can also read 12 project ideas by pythonistaplanet by clicking here

I hope you have quite a good knowledge of how to think for a project and start building it. If you have any question or query then feel free to ask in the comment section and i will definitely answer. Thanks.

Continue Reading

Trending