Categories
Python Answers

How to get the current URL within a Python Django template?

Sometimes, we want to get the current URL within a Python Django template.

In this article, we’ll look at how to get the current URL within a Python Django template.

How to get the current URL within a Python Django template?

To get the current URL within a Python Django template, we can use request.path or request.get_full_path.

For instance, we write

{{ request.path }}  
{{ request.get_full_path }}  

in our template to render the URL without query parameters with request.path.

And we render the full URL with the query parameters with get_full_path.

Conclusion

To get the current URL within a Python Django template, we can use request.path or request.get_full_path.

Categories
Python Answers

How to create a slug in Python Django?

Sometimes, we want to create a slug in Python Django, we can use the slugify function.

In this article, we’ll look at how to create a slug in Python Django, we can use the slugify function.

How to create a slug in Python Django?

To create a slug in Python Django, we can use the slugify function.

For instance, we write

from django.template.defaultfilters import slugify
slugify("b b b b")

to call slugify to return a string with the spaces between the b’s filled dashes.

Conclusion

To create a slug in Python Django, we can use the slugify function.

Categories
Python Answers

How to redirect to named url pattern directly from urls.py in Python Django?

To redirect to named url pattern directly from urls.py in Python Django, we can call the RedirectView.as_view method.

For instance, we write

from django.views.generic import RedirectView

urlpatterns = patterns('',
    url(r'^some-page/$', RedirectView.as_view(pattern_name='my_named_pattern', permanent=False)),
    #...
)

to add the some-page URL into the urlpatterns.

In it, we call RedirectView.as_view to redirect to the view with the pattern name.

And we set permanent to set whether we want the redirect to be permanent.

Categories
Python Answers

How to override default queryset in Python Django admin?

To override default queryset in Python Django admin, we can override the get_queryset method in our model.

For instance, we write

class MyModelAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(MyModelAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(author=request.user)

to add the get_queryset into MyModelAdmin.

In it, we call call the super constructor with MyModelAdmin and self.

And we get the queryset wirth get_queryset from the super class.

And then we return the queryset we want according to the value of request.user.is_superuser.

Categories
Python Answers

How to change the default Python Django date template format?

To change the default Python Django date template format, we can use the date filter.

For instance, we write

<p>Birthday: {{ birthday|date:"M d, Y" }}</p>

in our template to change the date format of birthday with the date filter.

We should get something like

Jan 29, 2000

rendered