Dynamic Links in Django Django by Ravinder Nath Rajotiya - January 5, 2022January 6, 20220 Introduction Let us take an example of the blog post entries on a web page. If there are lot many blog posts, it would become very difficult to scroll up and down to read a post. Instead, it would be much better and aneasier way of accessing the post we could for examle pass the blog id in url e.g. localserver:8000/1 or localserver:8000/4 etc. This will directly take you to bolog id ‘1’ or blog id ‘4’. This is also the case with twitter or facebook etc when people say follow me @name. Since there are huge number f users and username were displayed on the page to select from, it would not be possible for us to querry/serch and access the
blogApp – Forms and database connection Django by Ravinder Nath Rajotiya - January 5, 2022January 5, 20220 blogApp - Forms and database connection In this post we are going to see how to connect forms with database in Django. We explain step by step method. Refer to previous post Simple blogApp in Django This post is extension of Simple blogApp in Django. The following files are created/updated Step-1: Create forms.py and define the fields for table from django import forms from .models import blogPostDb class blogposts(forms.ModelForm): class Meta: model = blogPostDb fields = ['title', 'author', 'contents'] Step-2: Modify the views.py from django.shortcuts import render, redirect from .models import blogPostDb from .forms import blogposts from django.contrib import messages # Create your views here. def home(request): if request.method == 'POST': form = blogposts(request.POST or None) if form.is_valid(): form.save() all_posts=blogPostDb.objects.all() messages.success(request,('Post added.....')) return render(request, 'blog/index.html', {'all_posts': all_posts}) else: all_posts=blogPostDb.objects.all() return render(request, 'blog/index.html', {'all_posts': all_posts}) Step-3: Now modify the home page (index.html) <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title></title> </head> <body> <h1>Welcome home</h1> {% for
Simple blogApp in Django Django by Ravinder Nath Rajotiya - January 4, 2022January 5, 20220 Steps in creating blogProject Starting virtual environment E:\project\webAppProjects>python –m venv venv E:\project\webAppProjects>venv\Scripts\activate Creating Project (venv) E:\project\webAppProjects>django-admin startproject blogProject (venv) E:\project\webAppProjects>cd blogProj (venv) E:\project\webAppProjects\blogProject>django-admin startapp blogApp Run the Server (venv) E:\project\webAppProjects\blogProj>python manage.py runserver Watching for file changes with StatReloader Performing system checks... January 04, 2022 - 09:31:14 Django version 4.0, using settings 'blogProj.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. now the server is ready, you can test by typing the url http://127.0.0.1:8000/ in the browser Developing Project in Atom Open the Project folder in Atom, your window will look as: Now modify urls.py and settings.py in blogProj folder Now create url.py in blogApp and update url.py and views.py as shown below: Create a folder template in blogApp and create a index.html
Multi Page Website in Django Django by Ravinder Nath Rajotiya - December 30, 20210 Project – Multi page website We will following steps in creating a multi-page website in Django Steps-1: Create views.html Step-2: create path in urls.py Step-3: create a template folder in external folder Step-4: create html file index.html and contact.html files in the template folder Step-5 : run the project in the browser Steps-1: Create views.html From django.shortcuts import render def home(request): return render(request,’index.html’) def contact(request): return render(request,’contact.html’) Step-2: create path in urls.py from . import views urlpatterns = [ path(‘admin/’, admin.site.urls), path(‘ ‘, views.home), path(‘about/’, views.contact, name=’contact’), #check name=’anyname’ ] Step-3: create a template folder in external folder Right click external folder and right click to create folder, name it as template Step-4: create html file index.html and contact.html files in the template folder <!DOCTYPE html> <html lang=”en” dir=”ltr”> <head> <meta charset=”utf-8”> <title> home </title> </head> </body> <h1> This is my first page</h1> <h3> list </h3> <ul> <li>
Starting Django Computer Engineering Django by Ravinder Nath Rajotiya - December 29, 20210 Checking if Django is installed and working: Type the following on the command prompt to check whether Django is installed or not C:\Users\ECE>django-admin C:\Users\ECE>'django-admin' is not recognized as an internal or external command, operable program or batch file. It simply is an indication that Django is not installed on your systems. So first of all create a python virtual environment so that you can install Django and start working on the Django environment. Steps for creating virtual environment Example: Go to the Project Folder:- E:\project\webAppProjects Set virtualEnv as : E:\project\webAppProjects>python -m venv venv Activate environment : E:\project\webAppProjects>venv\Scripts\activate Install Django Then install Django in that environment (venv) E:\project\webAppProjects\webProject1>cd \ (venv) E:\>pip install django Now you are ready to start Django for creating web applications Working with Django:- here is the example of