Django render different templates for two submit buttons in a form
<p>I am a beginner at Django development, and I am trying to make a food diary application. After a user enters his email on <code>index.html</code>, another web page should be rendered according to whichever button he clicks.</p>

<p>I can possibly add two templates, but I also want my app to work if a user manually types a valid URL such as <code>/apps/&lt;user_email&gt;/addDiaryEntry/</code>. I don't know what to add in <code>/apps/urls.py</code>. Also, can I somehow access a user object's Id so my routing URL become <code>/apps/&lt;user_id&gt;/addDiaryEntry/</code> instead?</p>

<p><strong>/templates/apps/index.html</strong></p>

<pre><code>&lt;form method="post" action="/apps/"&gt;
{% csrf_token %}

&lt;label for="email_add"&gt;Email address&lt;/label&gt;
&lt;input id="email_add" type="text"&gt;

&lt;button type="submit" name="add_entry"&gt;Add entry&lt;/button&gt;
&lt;button type="submit" name="see_history"&gt;See history&lt;/button&gt;
</code></pre>

<p><strong>/apps/views.py</strong></p>

<pre><code>def index(request):
    if request.POST:
        if 'add_entry' in request.POST:
            addDiaryEntry(request)
        elif 'see_history' in request.POST:
            seeHistory(request)

    return render(request, 'apps/index.html');

def addDiaryEntry(request):
    print ("Add entry")

def seeHistory(request):
    print ("See history")
</code></pre>

<p><strong>/apps/urls.py</strong></p>

<pre><code>urlpatterns = [
    url(r'^$', views.index, name='index'),
]
</code></pre>

<p>Thank you for your help! Please feel free to share any best practices which I am not following.</p>

<p>1) passing in an argument into a url, you can use regex groups to pass arguments. Here is an example using a kwarg:</p>

<pre><code>url(r'^(?P&lt;user_email&gt;[^@]+@[^@]+\.[^@]+)/addDiaryEntry/$', views.add_diary, name='add-diary-entry'),
</code></pre>

<p>2) just render a different template depending on which button was pressed:</p>

<pre><code>def index(request):
    if request.POST:
        if 'add_entry' in request.POST:
            addDiaryEntry(request)
            return render(request, 'apps/add_entry.html');

        elif 'see_history' in request.POST:
            seeHistory(request)
            return render(request, 'apps/see_history.html');
</code></pre>

<p>It's always tough starting out, make sure you put in the time to go over the docs, here are some places to look over regarding these topics:
<a href="https://docs.djangoproject.com/en/1.10/topics/http/urls/#named-groups" rel="nofollow">https://docs.djangoproject.com/en/1.10/topics/http/urls/#named-groups</a>
<a href="https://docs.djangoproject.com/en/1.10/topics/http/views/" rel="nofollow">https://docs.djangoproject.com/en/1.10/topics/http/views/</a></p>

python django forms django-templates