Django media files not Loading when DEBUG = FALSE

Django media files not showing with Debug = False on production. It loads without css & media files like images.

This Code is Checked in Django version 2.1

In the url.py file add below code, to import the serve lib

from django.views.static import serve

Add the below codes in urls.py file (in settings.py containing directory)

if not settings.DEBUG:
    urlpatterns += [
        url(r'^uploads/(?P<path>.*)$', serve,{'document_root': settings.MEDIA_ROOT}),
        url(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),
    ]

Django- Set created & updated date/time in your models

In Django, many of us have question how to create date time in database automatically, When a new data is inserted or Existing Data is Updated.

So, Now we can use to have the date a record was added, or most recently updated. Django makes this really easy.

class Blogs(models.Model):
    title = models.CharField(max_length=100)
    added = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

How to Update the Current Browser URL

# UPDATE THE CURRENT URL IN BROWSER::
<script>

ChangeUrl = function(page, url) {

    if (typeof (history.pushState) != "undefined") {

        var obj = { Page: page, Url: url };

        history.pushState(obj, obj.Page, obj.Url);

        $('.breadcrumb-get-a-pro').attr('href', url);

    } else {

        alert("Browser does not support HTML5.");

    }

}

</script>

Sitemap in Python Django

Sitemap in Python Django

# For Sitemap
from django.views.generic import TemplateView
urlpatterns = [

    url(r'^admin/', admin.site.urls),

    #include testapp app url
    url(r'', include('testapp.urls')),

    url(r'^sitemap\.xml$', TemplateView.as_view(template_name='sitemap.xml', content_type='text/xml')),


]
**  keep the sitemap.xml file in app/templates/sitemap.xml