Thursday, November 2, 2017

My first project - URL issue?


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> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.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 %}


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8a38901b-1bf4-4337-b3e7-309b991cc727%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment