Re: My first project - URL issue?

2017-11-02 Thread Tony King

>
> 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 %}
{{ my_apps.app_name }}

$("#app{{ forloop.counter }}").click( function() {
$.post("{% url 'launch' %}", {appname: '{{ my_apps.app_name }}'}, function 
() {});
});

{% endfor %}

 Initially this moved me onto CSRF token errors but having temporarily 
> disabled this, my function has been executed.  Onto the next issueCSRF 
> tokens..
>

-- 
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+unsubscr...@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/82055563-01b3-4f46-a519-a1c5e15a1006%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


My first project - URL issue?

2017-11-02 Thread Tony King

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 %}




https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js";>



var csrf_token = '{% csrf_token %}';



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


{{ hdr1 }}

{% if my_apps_list %}

{% for my_apps in my_apps_list %}
{{ my_apps.app_name }}

$("#app{{ forloop.counter }}").click( function() {
// $("h1").hide();
$.post("launch_app{{ forloop.counter }}/", {appname: '{{ my_apps.app_name 
}}'}, function () {});
});

{% endfor %}

{% else %}
No applications are available.
{% 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+unsubscr...@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.