Django, external link url amended to project url - django

I have a Django project and want to take the user to an external link in openstreetmaps that is supplied lat and long from my Django model. In my view, I develop a url like this: "http://www.openstreetmap.org/?minlon=-92.024&minlat=57.5129102&maxlon=-91.52&maxlat=43.00&mlat=42.899&mlon=-91.774" and I provide it as a variable in my template. The link in my template is:
<a id="map" target="_blank" href={{mapurl}}>map</a>. {{mapurl}} is the url fed from my view.
The problem is that Django tries to find a url conf that looks like:
http://127.0.0.1:8000/beach/1825108/%22http://www.openstreetmap.org/?minlon=-92.0240039&minlat=42.78940921&maxlon=-91.5240039&maxlat=43.00940921&mlat=42.89940921&mlon=-91.7740039%22
rather than taking the user to the openstreetmap in a new page.
Any thoughts?

You can write the URL directly in the template
eg
<a id='map'
target='_blank'
href='http://www.openstreetmap.org/?minlon={{minLon}}&minlat={{minLat}}&maxlon={{maxLon}}&maxLat={{maxLat}}&mlon={{mLon}}&mlat={{mLat}}'
>Map</a>
You'd just have to pass the various variables in the template context

Related

How to link tags to search results in a Django Blog?

I am building a blog in django and am using django-taggit. I'm trying to figure out how to tie the link of a tag to a search result page that shows all of the posts using that tag.
I already have the search page created, I know how to filter queries to the page to bring the correct results, and know how to link to the page itself {% url 'search' %}. But how would I pass queries to the page from the template?
For instance, if I have a post tagged "dog" I want users to be able to click on the tag "dog" and be taken to the search page that only has results for posts also tagged "dog".
The django documentation for class views does not have examples of this. And every tutorial resource so far has been focused on the filtering and displaying of the search page itself rather than linking to it with desired queries in an <a> tag instead of a <form>.
In short, how do you make an <a> link pass a query into a url, like how an <input> would to a <form> action in Django?
I found the answer. Based on this , it is
<a href="{% url 'myview' %}?q=foobar">

Add url parameters to a url in django view

I have a django view that lists several urls on external sites.
When I render them I would like to add a few url parameters to each.
These urls are to an external system and thus not listed in my urls.py. Furthermore, some of the links have a hash '#' so it is not as easy as appending a few parameters to the end of the string.
Based on these requirements it seems the url template tag will not be a good fit. I was wondering if there is a custom filter out that to do this.
You don't need Django's url tag here. The url tag is to resolve to URLs that belong to your application.
However, there is the nice django-spurl library. It allows you to handle query parameters via template tags.
An example from the documentation to add query parameters:
{% spurl base="http://example.com/?foo=bar" add_query="bar=baz" %}
<!--
will result in: http://example.com?foo=bar&bar=baz
-->

django url parse formatted url

I'm in the design stages of a single page web app, and would like to make it so that a user can click on a formatted URL and the data requests will load in the page.
For example, a url of http://www.mysite.com/?category=some_cat will trigger the Category view with the relevant data.
My intention is to parse the URL, gather the data, then pass it to the index.html template for rendering on page load. Once the page has been loaded, a Javascript trigger setting will trigger the appropriate button to load the client view.
However, I'm having an issue setting up the URL parser, as the following settings are not matching the example url above.
from app.views import app_views, photo_views, user_views, admin_views
urlpatterns = patterns("",
url(r'^/(?P<category>\d+)/$', app_views.index)
)
You're confusing between sending information through your urls with GET and formatting you urls with arguments for the view functions. Say I am visiting a site called http://www.mysite.com/ and the page has a form that looks like this:
<form>
<input type='text' name='category' id='category'></input>
<button type='submit'>Send!</button>
</form>
upon clicking, the url will automatically change to http://www.mysite.com/?category=<value of input>. The ? marks that everything afterwards should be treated as GET data, with the syntax of <id>=<value>. You can then access them like so:
def response(request):
category = request.GET['category']
formatting urls is different, because it means looking for patterns that are part of the url. i.e. a pattern that looks like r'^/(?P<category>\d+)/$' will look for this: http://www.mysite.com/<category>/ and it will send it to the request in your views as an additional argument like so:
def response(request, category):
...
The regex is used to define how you recognize that part of the url. For example, the \d+ you're using means that category needs to be a number. You can search how to define different types of patterns according to your needs
Note that with GET you are sending the data to the same view function that rendered the page you are currently visiting, while using a different url means you tell it where to go through your urls.py (usually a different function). Does that make things a bit clearer?

Using Django URLs with get query strings

I use the url template tag with named URLs all the time in Django but now I need to be able to pass query strings to a couple of URLs in a Django template. Is this possible to do? The url template tag documentation does not mention GET query strings at all and hardcoding in URLs is bad practice if you ever want to change the URL in the future you also need to change the templates.
Well, I do not quite understand but... I think you want something like this:
<a title="" href="{% url my_url %}?foo=bar&spam=eggs">My link</a>

View referenced by two urls and url tag

I am using url tag in my template for a view, that is used by two different urls. I am getting the wrong url in one place. Is there any way to force django to retrieve different url? Why it doesn't notify my, that such conflict occured and it doesn't know what to do (since python zen says, that is should refuse temptation to guess).
Code in template:
{% url djangoldap.views.FilterEntriesResponse Entry=entry.path as filter_url %}
Code in urls:
(r'^filter_entries/(?P<Entry>.*)/$',
'djangoldap.views.FilterEntriesResponse',
{'filter_template': 'filter_entries.html',
'results_template': 'filter_results.html'}),
(r'^choose_entries/(?P<Entry>.*)/$',
'djangoldap.views.FilterEntriesResponse',
{'filter_template': 'search_entries.html',
'results_template': 'search_results.html'}),
As you can see, those two urls use the same view, but with different templates. How I can force django to retrieve former url, rather than latter?
Name your URLs by adding another item to the tuple:
(r'^choose_entries/(?P<Entry>.*)/$',
'djangoldap.views.FilterEntriesResponse',
{'filter_template': 'search_entries.html',
'results_template': 'search_results.html'},
'sensibleprefix-choose_entries') # <-- this is the name
Then you can use the name in the URL tag.