Tuesday, January 3, 2023

How to Custom Middleware in Django

 How to Custom Middleware in Django


To create a Django middleware for getting the user's IP address, you will need to do the following:

  • Create a new Python file in your Django project's middleware directory.
  • Define a new middleware class that inherits from MiddlewareMixin.
  • Override the __init__ and process_request methods.
Here is an example of what the middleware class might look like:

from django.utils.deprecation import MiddlewareMixin

class getIPMiddleware(MiddlewareMixin):
    def __init__(self, get_response):
        self.get_response = get_response

    def process_request(self, request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            user_ip = x_forwarded_for.split(',')[0]
        else:
            user_ip = request.META.get('REMOTE_ADDR')
        request.user_ip = user_ip

To use this middleware, you will need to add it to the MIDDLEWARE list in your Django settings. For example:

MIDDLEWARE = [    ...    'djangoapp.middleware.getIPMiddleware',]


With this middleware in place, you can access the user's IP address by calling request.user_ip in your Django views.



Thank you please feel command for existing more django content



Follow up for More Contents

Thursday, May 20, 2021

How to Send Email using Django

How to Configure Django Local Mail Server 

Hey guys let's start to few lines of code can send email
send_mail import this package top of the code section.
from django.core.mail import send_mail
The below code to paste where you want
send_mail(
    'Your subject',
    'Your message body',
    'from@mail.com',
    ['to@mail.com'],
    fail_silently=False,
)
Settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Thank you.Happy coding!

How to Create Django Forms

 How to Create Django Forms


Django Forms:
    Django forms is a collection of html elements.html forms help to reduce html codes and user friendliness.
    Forms contain lot of elements.User can create default django forms.
Go to the Django app create forms.py

from django import forms

class NameForm(forms.Form):
	Name = forms.CharField(label='Enter your Name', max_length=100)
First you need to import forms funtion.Then "NameForm" is your choose name.That just a example.
Thank you. Happy Coding!

How to Create Django Models

 How create Django Models


Django Models:
    Django Models is method used to create database.One class contains one database table.

Go to models.py
from django.db import models

class BlogModel(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    
    def __str__(self):
        return self.title
    Everything before use need to import package
    BlogModel is choosable name.What ever name your want will give that.Then send Model argument to that class.
    Inside of the class i define two fields.One is the title another one is a content.
    Django models return a objects .So we need to convert objects to string.
Thank you.Happy coding!


How to configure Django App in Django Project

 How to configure Django App in Django Project



How to configure Django app in Django project.
Why its need ?
    Because project must connected to app.Project asks  hey i need that particular function please give.If you have that function give me or send error.
    Also maintaining a Django app in case future it will be easy to debug.Then Most of the developer work together easily work.Then fastly they do our works

Go to settings.py file.
Then scroll down there is available section INSTALLED_APPS
connect name like 'blog'.
blog is the our django.
we can connect that name with strings format

project-settings.py-installed_app


Thank you.Happy coding!




Wednesday, May 19, 2021

How to Setup Static and Media Files in Django

How to Setup Static and Media Files in Django


 

Settings.py
import os
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
 
#import files
from django.conf import settings
from django.conf.urls.static import static

#paste it bottom of the url

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Open Terminal:
python manage.py collectstatic
Thank you .happy coding!