Share and Learn To Write Code!

Programming articles, which help you to build your application. If something you are stuck to code and complete the program, just find here your question-related articles.

Share with   

How to create Dajngo Site for django sitemap generator to create dynamic sitemap.xml file

Here we are creating Django Site which helps for creating sitemap.xml in django python.
Django   Django Sitemap   Django Site   Google Sitemap   Django SEO  

Django Site


Django Site is optional site framework. It is used to holding domain name and verbose name of your django or any site.

This is used when if you want to create more than one domain for example another domain is used to create another domain urls for sitemap or location anything. Then this is useful.


Prerequisite

  1. Install Python
  2. Install Django click for how to install Django and python.



Here we are seen example for to create custom domain for generating urls for sitemap.xml file which is used for create google sitemap.


Step 1

Register Site Framework to django settings.py file

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites'
]


Step 2

Create domain site having two ways

  1. Create Site with register site framework to admin and add or edit site.
  2. Create site with python shell


1) Create Site with register site framework to admin and add or edit site.

from django.contrib import admin
from django.contrib.sites.models import Site

admin.site.unregister(Site)

class SiteAdmin(admin.ModelAdmin):
    fields = ('id', 'name', 'domain')
    readonly_fields = ('id',)
    list_display = ('id', 'name', 'domain')
    list_display_links = ('name',)
    search_fields = ('name', 'domain')

admin.site.register(Site, SiteAdmin)


To see sites open browser and paste this url

http://127.0.0.1:8000/admin


Output : -

modified admin page showing Site records' id


2) Create site with python shell

Follow the commands to create new site

python manage.py shell

This will open shell prompt to add python commands interface

from django.contrib.sites.models import Site  # Press  Enter

new_site = Site.objects.create(domain='procodeprogramming.com', name='procodeprogramming.com') # Press  Enter

print new_site.id # Press  Enter

2


Now use this 2 as your site id and register it to settings.py file


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites'
]

SITE_ID = 2


Your Site is created successfully. Now you can check in admin site by using step 1.


If you want to use this to create sitemap.xml file in python django go to link.


Related Articles :


How to create dynamic sitemap xml file in Python Django web framework for SEO


Hit Like

Hit like for this post

Here About Author
Author Image
Pro Code Programming

Programming articles, which help you to build your application. If something you are stuck to code and complete the program, just find here your question-related articles.

0replies
Leave a reply
43
Registerd Users
84
Total Subscribers
59
Total Articles
30619
Total Views