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

Categories
Python Answers

How to iterate through dictionary in a dictionary in a Python Django template?

To iterate through dictionary in a dictionary in a Python Django template, we can loop through the items with a for loop.

For instance, we write

<table>
    <tr>
        <td>a</td>
        <td>b</td>
        <td>c</td>
    </tr>

    {% for key, values in data.items %}
    <tr>
        <td>{{key}}</td>
        {% for v in values[0] %}
        <td>{{v}}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</table>

to loop through the data dict’s entries with a for loop.

We get the key value pairs with data.items.