Tanaka Ryuu

Tanaka Ryuu

1563778777

Creating sitemap of Dynamic URLs in your Django Application

A site map can be a hierarchical list of pages, an organization chart, or an XML document that provides instructions to search engine crawl bots.

Why sitemaps are required:

XML Sitemaps are important for SEO because they make it easier for Google to find your site’s pages—this is important because Google ranks web PAGES, not just websites. There is no downside of having an XML Sitemap and having one can improve your SEO, so we highly recommend them.

Example:

The sitemap for this blog can be found at http://thepythondjango.com/sitemap_index.xml .

Steps to add Sitemaps to your Django Application:

Create a file sitemap.py in your app.

Create two different classes in sitemap.py file, one for static pages and another for Dynamic URLs.

Let’s assume your website sell some product where product details are stored in the database. Once a new product is added to the database, you want that product page to be searchable by search engines. We need to add all such product pages/URLs to sitemaps.

Static Sitemap:

Define a class StaticSitemap in your sitemap.py file. Define the mandatory function items in it which will return the list of objects.

These objects will be passed to the location method which will create URL from these objects.

from django.contrib.sitemaps import Sitemap
from django.core.urlresolvers import reverse

class StaticSitemap(Sitemap):

    def items(self):
        return [
            'myapp:terms_and_conditions',
            'myapp:contact_us',
            'myapp:about_us'
        ]

    def location(self, item):
        return reverse(item)

Here in items function, we are returning appname:url_name which will be used by location method to convert into an absolute URL.

Refer you app’s urls.py file for URL names.

Dynamic Sitemap:

Similarly, we will create Dynamic sitemap by fetching values from DB.

from mystore.models import ProductDetailsModel


class ProductSitemap(Sitemap):

    def items(self):
        return ProductDetailsModel.objects.all()

    def location(self, item):
        return reverse('myapp:product', args=[item.product_id])

Here we are getting all products from the database and generating URLs like http:example.com/product/12.

Adding sitemaps in URLconf:

Now add these sitemap class in URLconf. Edit the project’s urls.py file and add below code in it.

from mystore.sitemap import  StaticSitemap, ProductSitemap
from django.contrib.sitemaps.views import sitemap


sitemaps = {
 'pages': StaticSitemap,
 'products': ProductSitemap,
}

urlpatterns += [
    url(r'^sitemap.xml
Now reload your server and go to ```localhost:8000/sitemap.xml``` and you will be able to see your sitemap there.  

*Originally published **** ****at *[***pythoncircle.com***](https://www.pythoncircle.com/post/584/creating-sitemap-of-dynamic-urls-in-your-django-application/ "***pythoncircle.com***")

===================================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on [**Facebook**](https://www.facebook.com/Pythontutorial4u "**Facebook**") | [**Twitter**](https://twitter.com/codek_tv "**Twitter**")
## Learn More

☞ [Complete Python Bootcamp: Go from zero to hero in Python 3](http://learnstartup.net/p/S15_M7e-l "Complete Python Bootcamp: Go from zero to hero in Python 3")

☞ [Python and Django Full Stack Web Developer Bootcamp](http://academy.learnstartup.net/p/r1-quFMgce "Python and Django Full Stack Web Developer Bootcamp")

☞ [Python for Time Series Data Analysis](http://learnstartup.net/p/OqjO6VL2y "Python for Time Series Data Analysis")

☞ [Python Programming For Beginners From Scratch](http://learnstartup.net/p/d3qnutpk4 "Python Programming For Beginners From Scratch")

☞ [Python Network Programming | Network Apps & Hacking Tools](http://learnstartup.net/p/QRQDxXvpZ "Python Network Programming | Network Apps & Hacking Tools")

☞ [Intro To SQLite Databases for Python Programming](http://learnstartup.net/p/7u6CWaas2 "Intro To SQLite Databases for Python Programming")

☞ [Ethical Hacking With Python, JavaScript and Kali Linux](http://learnstartup.net/p/aRaBXiAoZ "Ethical Hacking With Python, JavaScript and Kali Linux")

☞ [Beginner’s guide on Python: Learn python from scratch! (New)](http://learnstartup.net/p/szT1QzYPf "Beginner’s guide on Python: Learn python from scratch! (New)")

☞ [Python for Beginners: Complete Python Programming](http://learnstartup.net/p/LMc19YJl6 "Python for Beginners: Complete Python Programming")

☞ [Django 2.1 & Python | The Ultimate Web Development Bootcamp](http://academy.learnstartup.net/p/rJmJxyM9M "Django 2.1 & Python | The Ultimate Web Development Bootcamp")

☞ [Python eCommerce | Build a Django eCommerce Web Application](http://academy.learnstartup.net/p/H19Qb1nkf "Python eCommerce | Build a Django eCommerce Web Application")

☞ [Python Django Dev To Deployment](http://academy.learnstartup.net/p/9yr7V_-O8 "Python Django Dev To Deployment")
, sitemap, {'sitemaps': sitemaps})
]

Now reload your server and go to localhost:8000/sitemap.xml and you will be able to see your sitemap there.

#python #django #web-development

What is GEEK

Buddha Community

Creating sitemap of Dynamic URLs in your Django Application
Ahebwe  Oscar

Ahebwe Oscar

1620177818

Django admin full Customization step by step

Welcome to my blog , hey everyone in this article you learn how to customize the Django app and view in the article you will know how to register  and unregister  models from the admin view how to add filtering how to add a custom input field, and a button that triggers an action on all objects and even how to change the look of your app and page using the Django suit package let’s get started.

Database

Custom Titles of Django Admin

Exclude in Django Admin

Fields in Django Admin

#django #create super user django #customize django admin dashboard #django admin #django admin custom field display #django admin customization #django admin full customization #django admin interface #django admin register all models #django customization

How to create a Django project and a Django application

To start off create a new folder for the Django project and move into it
Here I called django-quick-start the main folder that will hold the project. Pay attention, this is not the actual Django project, but just its “home”. Once inside the new folder create a Python virtual environment and activate it (note, these are two distinct commands)
Pay attention again: django_quick_start now is the actual Django project created inside the django-quick-start folder. The name of this home folder doesn’t matter, but Django projects cannot have dashes in the name. That’s the reason for these underscores in django_quick_start.
Pay also attention to the dot in django-admin startproject django_quick_start … With the dot we can avoid additional folder nesting.

#django #django application #create a django

Ahebwe  Oscar

Ahebwe Oscar

1620185280

How model queries work in Django

How model queries work in Django

Welcome to my blog, hey everyone in this article we are going to be working with queries in Django so for any web app that you build your going to want to write a query so you can retrieve information from your database so in this article I’ll be showing you all the different ways that you can write queries and it should cover about 90% of the cases that you’ll have when you’re writing your code the other 10% depend on your specific use case you may have to get more complicated but for the most part what I cover in this article should be able to help you so let’s start with the model that I have I’ve already created it.

**Read More : **How to make Chatbot in Python.

Read More : Django Admin Full Customization step by step

let’s just get into this diagram that I made so in here:

django queries aboutDescribe each parameter in Django querset

we’re making a simple query for the myModel table so we want to pull out all the information in the database so we have this variable which is gonna hold a return value and we have our myModel models so this is simply the myModel model name so whatever you named your model just make sure you specify that and we’re gonna access the objects attribute once we get that object’s attribute we can simply use the all method and this will return all the information in the database so we’re gonna start with all and then we will go into getting single items filtering that data and go to our command prompt.

Here and we’ll actually start making our queries from here to do this let’s just go ahead and run** Python manage.py shell** and I am in my project file so make sure you’re in there when you start and what this does is it gives us an interactive shell to actually start working with our data so this is a lot like the Python shell but because we did manage.py it allows us to do things a Django way and actually query our database now open up the command prompt and let’s go ahead and start making our first queries.

#django #django model queries #django orm #django queries #django query #model django query #model query #query with django

7 Mistakes You Should Avoid While Building a Django Application

Django…We all know the popularity of this Python framework. Django has become the first choice of developers to build their web applications. It is a free and open-source Python framework. Django can easily solve a lot of common development challenges. It allows you to build flexible and well-structured web applications.

A lot of common features of Django such as a built-in admin panel, ORM (object-relational mapping tool), Routing, templating have made the task easier for developers. They do not require spending so much time on implementing these things from scratch.

One of the most killer features of Django is the built-in Admin panel. With this feature, you can configure a lot of things such as an access control list, row-level permissions, and actions, filters, orders, widgets, forms, extra URL helpers, etc.

Django ORM works with all major databases out of the box. It supports all the major SQL queries which you can use in your application. Templating engine of Django is also very, very flexible and powerful at the same time. Even a lot of features are available in Django, developers still make a lot of mistakes while building an application. In this blog, we will discuss some common mistakes which you should avoid while building a Django application.

#gblog #python #python django #building a django application #django #applications

Kacey  Hudson

Kacey Hudson

1615513653

What is Django Used For? The Six Most Common Applications | Hacker Noon

Django is a web app framework based upon Python, but is not a part of Python. Python being open-source language, easy to learn and adapt, has become the programming language of choice for beginners as well as seasoned developers in recent years. Django was created to support web application development, web APIs, web services. Thanks to rising popularity of Python, Django too became a framework of choice for python developers.

But, why Django specifically? Why are popular social networking sites, online magazines and e-commerce sites using Django? Why is it so popularly used by developers? Let’s find out what Django is used for.

6 Ways Developers are Using Django

  • For scalable web applications
  • Django is used for applications with multiple user roles
  • For building speedy SaaS applications
  • Secure E-commerce and Enterprise applications
  • Django is Used for a building low cost MVP
  • For building Cross-platform applications

#django #django-in-mvp #django-in-saas #resize-image-in-django #django-tips