Link to get http response works just once in django - python

I am having problem to get graphical result for second time either it be for a database query or two or more queries. For first time result is given as desired immediately but thereafter browser just says 'Connecting'. For example after http://localhost:8000/graph/ gives pie-chart for first time it does not give the pie-chart again when the same link is hit and also any other similar link http://localhost:8000/graph2/ doesnot work. The latter link would have worked if http://localhost:8000/graph2/ was hit first after execution of program.
In urls.py links are given as:
url(r'^graph/', graph, name = 'ngraph'),
url(r'^graph2/', graph2, name = 'ngraph2'),
Directory structure looks:
Detail code is given HERE.

Two options:
In your urls.py, add a separate pattern object with the prefix set as the views of your app. That would be like urlpatterns += patterns('welcome.views', url(r'^graph/', ...)). Rest should be as it is.
Dont change urls.py. Instead refer to the complete path in the template starting from your app. In your case that might be {% url welcome.views.ngraph %}.
Since you mentioned that GET is not being served at all, it is quite possible that the server cant locate (map) the url to the function.
In an ideal situation, you should import welcome.views instead of using from .. import * and polluting the urls.py globals. Use the complete path in the patterns object. This way you wont need separate patterns objects for different apps. This would make a lot of sense in a bigger project with more applications. But in any case, this is the standard practice and is encouraged.
Do post your result.

Related

I get this error when I try to render some html files in django: NoReverseMatch at / Reverse for 'it_languages' not found

When I try to render a html file in django project, I get this error, and I can't see the localhost page 'cause of this.
The error is:
NoReverseMatch at / Reverse for 'it_languages' not found. 'it_languages' is not a valid view function or pattern name.
and it languages is url for in html
Then it bolds me with yellow this:
Home
About Me
**IT-languages**
Projects
Contact
I expect to see my offline page rendered by django project
Should I keep it like the original html version:
Home
About Me
IT-languages
Projects
Contact
Please start by providing your urls.py, views.py, and the it_languages template.
As #cbirch said, it's impossible to exactly provide an answer without more information (preferably the code they listed).
In general, the error 'NoReverseMatch' happens when you are trying to refer to an endpoint by the name, but the name doesn't map to a provided endpoint.
When you create a route you add an entry to the urlpatterns list in urls.py (typically) - if you don't give them a specific name, they will be given an automatically generated name. Then in your template file, if you refer to one of these names, they have to match!
Probably you use { url it_languages } or similar in one of your templates, but you haven't set that as a name for a url.
If you install django-extensions, there's a tool (show_urls) that will allow you to list your endpoints and their names (I believe) - that may help you trace the issue down. Good luck!

Flask and obtaining a full absolute path to a route

I'm looking to integrate some API's through a web application interface, to achieve this I need to identify the absolute path for a specific route. As I dont want to hard code any urls I'd like to do this dynamically in the flask app.
I thought that should be simple enough and tried a couple of things:
abs_url = url_for('home')
This didn't work of course as it just returned the route. I since learnt that url_for requires the function name. It then returns the route ('/home' for example).
I then tried:
abs_url = redirect(url_for('home'))
This gave me a redirect object, I was hoping path may be a variable I could call, however that doesnt seem to be the case.
Google has not been my friend today and hasn't given me anything to go on and advice would be appreciated.
The more I think about this the more I think the simplest option would be to capture the application address as part of the configuration.
To resolve this I have captured the application address as part of the initial info gathering form.

Django urls.py, what does the name parameter do?

I was poking around my friends project and as I looked through the urls.py file i noticed this:
url(r'^apply/$', contact.as_view(), name='careers_contact'),
I just recently learned about class based views, and it all makes sense to me except for the last bit name='careers_contact'. I also can't seem to find the meaning of this online.
Can someone shed light on what this is, where that name lives, and what its doing?
url() name parameter
"What is it? Where does it live?"
url() is simply a function that returns a django.core.urlresolvers.RegexURLPattern object, so passing in a name='careers_contact' argument sets name for that object. None of that is really relevant until this url(...) is placed into a URLconf.
THEN, if we need the URL of a view, we can now get it by passing that name into {% url 'careers_contact' %} in templates or reverse('careers_contact') in code and on the backend those functions will use the name to map back to the correct URL.
Why do we need it?
We can reverse the Python Path to get the URL (ex. reverse(blog.views.home) ), so what's the point in using name?
URL Naming and Namespacing allow for 3 things:
A simple way to reverse URL match Class-Based Views. Though it is possible without it.
A way to distinguish URL patterns using the same view and parameters.
A way to differentiate URL names between apps.
(Click the links for an example of the issue and how naming/namespacing solves it)
The reason they probably added a namespace for the URL is so that they can do reverse namespaced URL.
For example in a template somewhere, you will probably see something like:
Click me!

Django Redirect URL

I am new to Django, and i am now trying to use the HttpResponseRedirect() function. But I am so confused that if I use it like HttpResponseRedirect('good/'), and the current page is '/bad/', it can only be redirected to '/bad/good/', which is an url of current page appended with the url value from the HttpResponseRedirect() function. I tried to search google, and could not find any solution.
How can I redirect to the page with specific url? For example, HttpResponseRedirect('/good/') to /good/ rather than /bad/good/ ?
Surely you must see that there's a difference between 'good/' and '/good/'? The former will always add itself onto the existing page, whereas the latter will start from the root. This is basic web behaviour, and nothing to do with Django.
In any case, you should never hard-code URLs like that, but should use Django's URL-reversing functionality to calculate the URLs dynamically.
If you want to redirect to urls in your domain, you can use redirect. You can use redirect to view with by using patterns in your urls.py file or to any urls you 'd like. Although I strongly encourage the usage of views as indicates the different tutorials available on their website.
Better check out django documentation that is one of the most complete out there (to my humble opinion)
/edit for lack of clarity indeed.

Dynamically reload the URLConfs for a running site

I have a django site. Like all standard sites, it uses URLConfs to associate URLs with views. However, in addition to that, I have some URL configs which I dynamically generate from data in the database. Everything works as I would like it.
Is it possible to reload all the URLConfs while the site is running, from code? In case someone updates the database and change some of the URLs in the site, I would like to trigger a "rediscovery" of all the URLs. This would cause my code to dynamically re-create the URLs from the data in the DB.
Currently, the generated URLs can be anywhere in the URL hierarchy. They are not all under one prefix, such as /dynamic/ or such. However, if this is absolutely necessary to do what I need to get done, I can place all the dynamic URLs under one prefix.
Some downtime is allowed for the site while the rediscovery of URLs take place.
How would I trigger such a reloading of all the URLConfs?
Your question starts from a premise that most Django programmers wouldn't accept: that you can or should create URLs dynamically from the database. If you're doing that, you're doing it wrong.
Your URL patterns are part of the code, not the data. Obviously, the URLs themselves are formed by combining the patterns with the data - eg foo/<slug>/bar/, but this doesn't need reloading when new slugs are added because it is resolved by the view, not the URL processor.
import sys
from django.conf import settings
from django.core.urlresolvers import clear_url_caches
clear_url_caches()
reload(sys.modules[settings.ROOT_URLCONF])

Categories