My first project - URL issue?

17 views
Skip to first unread message

Tony King

unread,
Nov 2, 2017, 10:23:16 AM11/2/17
to Django users

Hi,

I've started my first real project (django v1.11.4 and Python 3.6.2) and I'm probably trying to run before I'm ready, but anyway I'm here.  I'm essentially trying to create a front-end menu, that will eventually launch and log-on to either a local application or another web-site.  This latter part I have managed in Python but not within a Django project.

Anyway, I have a model which hold details of my 3rd party applications and I've created an index view and template that will display a series of buttons as below.

I want to execute another function when I click on one of the buttons, the current idea is a function per button but I expect this to evolve into a single function, I'm just trying to get something to work at the moment.  I've found an example, involving jQuery, which I've attempted to adapt but I don't think I'm getting the URL right as nothing happens when I click one of my buttons.  I know the click event works, because if I leave in the $("h1").hide();, it works just fine.  So, I'm thinking I have the URL syntax wrong but as nothing happens, I'm not sure if I have any of the $.post quite right, although I receive no errors.

Having followed the tutorial I have stuck to the recommended folder layout as below;

project folder \
          project files
          shopfront \
                  templates \
                        static \
                        jquery-3.2.1.min.js
                  index.html
          urls.py
          views.py

Despite numerous edits, I don't seem to be getting anywhere - can someone tell me what I have wrong?

I'm going to need to create a front-end log-in page to this project, so if someone could point me in the right direction on doing this I would be grateful for that also.

urls.py
from django.conf.urls import url

from . import views

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^launch_app1/$', views.launch_app1, name='launch1'),
url(r'^launch_app2/$', views.launch_app2, name='launch2'),
url(r'^launch_app3/$', views.launch_app3, name='launch3'),
url(r'^launch_app4/$', views.launch_app4, name='launch4'),
url(r'^launch_app5/$', views.launch_app5, name='launch5'),
]


views.py
from django.shortcuts import render

from django.http import HttpResponse

from .models import my_apps

# Create your views here.
def index(request):
'''
Index page content function
'''
context = {'my_apps_list': my_apps.objects.all(), 'hdr1':'Application List'}
return render(request, 'shopfront/index.html', context)

def launch_app1(request):
'''
Launch application function
'''
# context = {'my_apps_list': my_apps.objects.all(), 'hdr1':'Application List - '+request['appname']+' Launched'}
context = {'my_apps_list': my_apps.objects.all(), 'hdr1':'Application List - Application 1 Launched'}
return render(request, 'shopfront/index.html', context)
# return HttpResponse(request['msg'])

def launch_app2(request):
'''
Launch application function
'''
return HttpResponse(request['appname'])

......... 


index.html
{% load static %}

<head>
<!-- <script src="{% static 'jquery-3.2.1.min.js' %}"></script> -->
<!-- <script src="static/jquery-3.2.1.min.js"></script> -->
</head>

<script>
var csrf_token = '{% csrf_token %}';
</script>

<script>
$("body").bind("ajaxSend", function(elm, xhr, s) {
if (s.type == "POST") {
xhr.setRequestHeader('X-CSRF-Token', csrf_token);
}
});
</script>

<h1>{{ hdr1 }}</h1>

{% if my_apps_list %}
<ul>
{% for my_apps in my_apps_list %}
<button type="button" id="app{{ forloop.counter }}">{{ my_apps.app_name }}</button><br><br>
<script>
$("#app{{ forloop.counter }}").click( function() {
// $("h1").hide();
$.post("launch_app{{ forloop.counter }}/", {appname: '{{ my_apps.app_name }}'}, function () {});
});
</script>
{% endfor %}
</ul>
{% else %}
<p>No applications are available.</p>
{% endif %}


Tony King

unread,
Nov 2, 2017, 1:18:28 PM11/2/17
to Django users
Ok, well I've solved my immediate problem but clearly need to learn more about JavaScript, jQuery and CSRF tokens.

But for anyone with a similar URL issue, I did the following;

Added an appname setting to my urls.py
appname = 'shopfront'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^launch_app/$', views.launch_app, name='launch'),
]

 Modified the $.post function to use the URL name mapping which as I expected meant I now have single function for handling the application launch.
{% for my_apps in my_apps_list %}
<button type="button" id="app{{ forloop.counter }}">{{ my_apps.app_name }}</button><br><br>
<script>
$("#app{{ forloop.counter }}").click( function() {
$.post("{% url 'launch' %}", {appname: '{{ my_apps.app_name }}'}, function () {});
});
</script>
{% endfor %}

 Initially this moved me onto CSRF token errors but having temporarily disabled this, my function has been executed.  Onto the next issue....CSRF tokens......
Reply all
Reply to author
Forward
0 new messages