Skip to content
Advertisement

Why doesn’t request.GET.get(“edit”) work in my code?

I’m trying to make a view in Django, in where if the edit button is pressed on that page it is passed on to another view as a session.

def page(request,user_input):
    entry = util.get_entry(user_input)
    name = user_input.capitalize()
    request.session['name'] = name
    request.session['entry'] = entry


    if request.GET.get("edit"): # **<- This is not working for some reason.**
        request.session['edit'] = True
    else:
        request.session['edit'] = False

 
    return render(request, "homepage/page.html", {
        "entry":entry,
        "title":name,
    })

Here is my page.html file

    {% block body %}

        {% if entry %}

            <h1>{{title}}</h1>

            {{ entry }}
            
    
        {% endif %}
  
        <form action="{% url 'create' %}" name='edit'>
        <input type="submit" value='edit' class="button">
        </form>

    {% endblock %}

This is the view where I want to use the session

def create(request):
    change =request.session['edit']
    if request.GET.get('submit'):
        title = str(title).lower()
        if change: 
            util.save_entry(title,content)
            return HttpResponseRedirect('/index')

Advertisement

Answer

You can specify a key-value pair by making use of the <button> tag, so then the form should look like:

<form action="{% url 'create' %}">
    <button name="submit" value="edit" type="submit" class="button">edit</button>
</form>
Advertisement