I am using Django 1.8 and Python 3.5
This is my urls.py file
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.conf import settings
urlpatterns = [
# Examples:
url(r'^$', 'mainpage.views.home2', name='home2'),
url(r'^currency/(?P<currencywa>[\w]+)', 'currency.views.curr1', name='curr1'),
url(r'^userpage/', 'login.views.userpage', name='userpage'),
]
I am trying to make links using Jquery for Django
what I want to do is this
test // this resides in a table
using jquery
I wish to use urlnames instead of url that is I wish to use name='curr1'
var typein="test";
$('#cryptotable > tbody:last-child').append('<tr id=\"5\"><td>data1</td> </tr>');
I want to do this equivalent in django
test
But when I click on this ,I get redirected to http://localhost:8000/{% url 'curr1'%} instead of http://localhost:8000/curr1}
What do I do to use Django way of url links made using Jquery ?
IN OTHER WORDS
I wish to do this (Dynamically using Jquery)-->
<html>
<body>
<a href='{% url "curr1" %}'>test</a>//put this duynamically using Jquery
</body>
</html>
I am just writing a sample template for general use case:
Template:
{% load staticfiles %}
<?xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>sample</title>
<script type="text/javascript" src="{% static 'jquery.min.js' %}"></script>
</head>
<body>
</body>
</html>
<script>
var typein = "<a href = {{url}}>Click Here</a>";
$('body').append(typein);
</script>
View:
from django.shortcuts import render_to_response
from django.template import RequestContext
def home(request):
url = "home"
return render_to_response('home.html', {'url' : url}, RequestContext(request))
URL:
from django.views.generic import TemplateView
from django.contrib.sitemaps.views import sitemap
from django.conf.urls import include, url
from django.contrib import admin
from views import *
urlpatterns = [
url(r'^$', home),
url(r'^home/', home),
url(r'^robots.txt/', TemplateView.as_view(template_name='robots.txt', content_type='text/plain')),
url(r'^sitemap.xml/', TemplateView.as_view(template_name='sitemap.xml', content_type='text/xml'))
]
The url template tag needs to be converted to a full URL during template rendering inside the server. Once the HTML reaches the client browser, javascript cannot make that transformation from url tag to full URL.
You could render the URL in a hidden part of your HTML document, so that javascript can access the URL later (in the browser) and place it inside a table, for example (like the other answer to this question shows in the example).
Related
views.py file:
from django.http import HttpResponse
from django.template import loader
def index(request):
template = loader.get_template('upload/testpage.html')
return HttpResponse(template.render)
app/templates/app/testpage.html file:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="file" id="upload" name="upload" style="visibility: hidden; width: 1px; height: 1px" multiple />
Upload
</body>
</html>
app/urls.py file:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),]
project/urls.py file:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^upload/', include('upload.urls')),]
Getting an error when running file on local server 127.0.0.1:8000/app of "TemplateDoesNotExist at /app/"
How can this be resolved?
With your current file path app/templates/app/testpage.html this one should works
def index(request):
template = loader.get_template('app/testpage.html')
return HttpResponse(template.render)
Another way to do it is to change your template path to app/templates/upload/testpage.html
I think you did not create any upload folder inside templtes folder.
If you have app folder like that
app/templates/app/testpage.html
Now, you can fix it
template = loader.get_template('app/testpage.html')
Try this,
from django.shortcuts import render
def index(request):
return render(request, 'app/testpage.html')
I'm starting in Django.
I'm trying to pass to my template my var to be shown in my browser but not working.
here's my views.py
from django.shortcuts import render
from django.http import HttpResponse
from preguntasyrespuestas.models import Pregunta
from django.shortcuts import render_to_response
# Create your views here.
def index(request):
string = 'hi world'
return render_to_response('test/index.html',
{'string': string})
here's my urls:
from django.conf.urls import *
from django.contrib import admin
from django.contrib.auth.views import login
from preguntasyrespuestas.views import index
urlpatterns = [
url(r'^$', index, name='index'),
]
my html:
<!DOCTYPE html>
<html>
<head>
<title> Preguntas </title>
</head>
<body>
<p>{{ string }}</p>
</body>
</html>
Basicaly I want to show what's in string in my template. but not working..
My error:
Using the URLconf defined in django_examples.urls, Django tried these URL patterns, in this order:
^$ [name='index']
The current URL, test/index.html, didn't match any of these.
What am I doing wrong? Thanks..
You should not add test/index.html at the end of your url in the browser, just something like http://127.0.0.1:8000/ and make sure that templates/test/index.html exists.
Django's url routing uses regular expressions to match routes.
url(r'^$', index, name='index'),
In this case you have just a single valid route, which is the empty string r'^$'. So you can only get a response by visiting for example http://localhost:8000. All other urls will fail.
Django's url routing is completely independent from where your template files are located on your file system. Therefore http://localhost/test/index.html will not be valid, even though there is a template file with that name.
You could make a catch-all route by using this pattern that will match any url path.
url(r'', index, name='index'),
I'm a beginner in django, I follow the steps showed at GitHub, but I just got a blank page, here my code:
views.py
from django.shortcuts import render
from django_ajax.decorators import ajax
#ajax
def AjaxView(request):
return render(request, 'blogs/ajaxtest.html'`
urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^ajax/', 'blogs.views.AjaxView', name='Ajax'))
ajaxtest.html
<head>
<script type="text/javascript" src="{% static '/blogs/django_ajax/js/jquery.ajax.min.js' %}"></script>
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<title>Ajax Test</title>
</head>
<body>
<script type="text/javascript">
ajaxGet('/', function(content){
//onSuccess
alert(content);
})
</script>
</body>
You need to include jQuery before django_ajax or django_ajax won't work, it is a requirement.
Also your AjaxView function seems to be incomplete.
I'm a newbie to Django and created a sample project with an app:
views.py
from django.http import HttpResponse,Http404
from django.contrib.auth.models import User
from django.template import Context
from django.template.loader import get_template
def main_page(request):
template = get_template('main_page.html')
variables = Context({
'head_title': u'Django Bookmarks',
'page_title': u'Welcome to Django Bookmarks',
'page_body': u'Where you can store and share bookmarks!'
})
output = template.render(variables)
print 'output',output
return HttpResponse(output)
urls.py
from django.conf.urls.defaults import patterns, include, url
from bookmarks.views import *
urlpatterns = patterns('',
(r'^$',main_page),
(r'^user/(\w+)/$',user_page),
#(r'login/$','django.contrib.auth.views.login')
)
main_page.html
<html>
<head>
<title>{{head_title}}</h1>
</head>
<body>
<h1>{{page_title}}</h1>
<p>{{page_body}}</p>
</body>
</html>
When I print the output variable, I see everything perfectly all right but when I run the server and point to the page, I see a blank page.
You are missing the closing tag </title>
Your template should look like this
<html>
<head>
<title>{{head_title}}</title>
</head>
<body>
<h1>{{page_title}}</h1>
<p>{{page_body}}</p>
</body>
</html>
I'm working through the random machine example, but I'm having trouble. I've installed Dajax and Dajaxice according to these two pages: Dajax and Dajaxice. My ajax.py file looks like this:
import random
from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register
#dajaxice_register
def randomize(request):
dajax = Dajax()
dajax.assign('#result','value',random.randint(1, 10))
return dajax.json()
Here's my views.py:
from django.shortcuts import render_to_response
from django.template import RequestContext
def dajax(request):
return render_to_response( 'dajax.html', context_instance=RequestContext(request) )
Here's my urls.py:
from django.conf.urls.defaults import patterns, include, url
from dajaxice.core import dajaxice_autodiscover
from dajaxice import urls
from django.conf import settings
from Dajax.myapp.views import dajax
dajaxice_autodiscover()
urlpatterns = patterns('',
(r'^%s/' % (settings.DAJAXICE_MEDIA_PREFIX), include('dajaxice.urls')),
(r'^dajax/$', dajax),
)
My html form tag looks like this:
<form>
<input type="text" name="result" value="" id="result">
<input type="button" name="rand" value="Let's Rand!" id="rand" onclick="Dajaxice.myapp.randomize(Dajax.process); return false;">
</form>
Here's what my head tag looks like:
<head>
<title>My base template</title>
{% dajaxice_js_import %}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript" charset="UTF-8"></script>
<script src="{{ STATIC_URL }}js/jquery.dajax.core.js" type="text/javascript" charset="UTF-8"></script>
</head>
I added the following to my init.py file in my project dir:
from myapp.ajax import randomize
Also of note, my Django app's name is myapp.
On my page, when I click on Let's Rand!, the page goes to http://localhost:8000/dajax/?result=2, and no value shows up in the text input box. I checked the console in Chromium and there aren't any errors.
Did I incorrectly install either Dajax or Dajaxice, or is there something wrong with my code above?
My guess is that because I'm using jQuery instead of Prototype, there's something different I need to do.
Thanks for your help!
You need a return false; in your onClick handler after the call to Dajax.
put this in your views.py :
from dajaxice.core import dajaxice_autodiscover
dajaxice_autodiscover()
You need to make sure that the app you are trying to import the ajak.py file from is in your INSTALLED_APPS in settings.py.
See the source for autodiscover:
http://docs.dajaxproject.com/dajaxice/_modules/dajaxice/core/Dajaxice.html