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)

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

Show Success or Failure message in Python Django

# Save Flash Message Data
messages.success(request, 'Settings Updated successfully.')
# Show Message in Html from Message variable
{% if messages %}

    {% for message in messages %}

        <div class="alert alert-success">

             <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>

             <strong>Success!</strong> {{message}}

        </div>

    {% endfor %}

{% endif %}
Or Like this,
<div class="col-sm-6">

    {% if messages %}

        <ul class="messages">

            {% for message in messages %}

                <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>

            {% endfor %}

        </ul>

    {% endif %}

</div>