GET and POST on Template Django.How

Author avatar wrote on 07/06/2022

If GET

{% if request.method == "GET" %} Get {% else%} post {% endif %}

Get a known var

{% if request.GET.my_var %}


Request.GET vars in Django template

{% for key, value in request.GET.items %}
    {{key}} = {{value}}
{% endfor %}

If having multiple values with the same key

{%for key, values in request.GET.lists%}
    {%for value in values%}
        {{value}}  
    {%endfor%}
{%endfor%}

If debug is true or false in a template

View

from django.conf import settings

def my_view(request):
    debug_flag = settings.DEBUG
    context = {'debug_flag':debug_flag}
    return render(request, "my_app/my_template.html", context )

Template

{% if debug_flag %}
    do something
{% else %}
    do something
{% endif %}

Resources
  • https://docs.djangoproject.com/en/3.2/ref/request-response/#django.http.HttpRequest
  • https://www.webforefront.com/django/viewmethodrequests.html
  • CRUDgen Django CRUD generator