Django child url does not show its template - python

I have two different urls:
1) /campaigns/, which lists available campaigns and 2)campaigns/add/ where there is a form to fill the details of a campaign to be created.
My django urls.py file looks like:
url(r'^campaigns/', views.campaigns, name='agency-campaigns'),
url(r'^campaigns/add/$', views.add_campaign, name='agency-add-campaign')
The problem is that whenever I browse the /campaigns/ url, it shows the correct template. But when I browse the /campaigns/add/ url, it still shows the /campaigns/ url template.
What can be wrong with this subdirectory?
Any ideas?
Thanks in advance!

That's because you don't have the end of string match ($) at the end of the regular expression:
url(r'^campaigns/$', views.campaigns, name='agency-campaigns'),
# HERE^
Or, you can also put the "agency-add-campaign" pattern before the "agency-campaigns":
url(r'^campaigns/add/$', views.add_campaign, name='agency-add-campaign'),
url(r'^campaigns/', views.campaigns, name='agency-campaigns')

Related

django find object from url

Supposing that I have something like that in my urls.py:
url(r'^task/(?P<pk>[0-9]+)/', taskview, name='taskview')
How can django help me extract '10' (or {"pk": 10}) from reading any matching URL string like:
/task/10/
/task/10///
/task/10/dddd/dddd/dddwwwww/wwwwq/qqqqqqw/qwead/?adawd=awodawdoij
?
(FYI when I say "reading any URL string" I mean that I am not receiving a request on those urls, but rather that I have such a string in my code on which I have to perform the aforementioned keyword extraction)
Just figured it out:
from django.urls import resolve
resolve(url).kwargs

i can't take the "?" character in the URL of django url

I try to code to django a redirect fonction. I provide the url, and i want to redirect to the provided URL.
in my urls.py:
urlpatterns = patterns('',
url(r'^redirect/(?P<name>.*)$', redirect),
# ...
)
when i test the function with a standard link (ex: google.com), it works perfectly.
when i test the function with a link that containt a "?" character, only the part before the "?" is taken into account.
Example :
"GET /redirect/http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing?id=53713291 HTTP/1.1" 302 0
name = http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing
the ?id=53713291 is not taken into account....
i though that .* means all kind character, wrong?
Do you know what happens ? and how to take the entiere url what ever the character it contains?
thank you very much for your help.
You seems to don't understand how URL works. Everything after the ? is parsed as arguments for your current view. If you print data in your request.GET dict, you'll find something like:
{'id': 53713291}
The only way to fix that is to urlencode your URL in the argument and decode it before the redirection.
>>> import urllib
>>> urllib.quote_plus("http://www.polyvore.com/lords_liverpool_hey_jude_tee/thig?id=53713291")
'http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291'
# You should make your URL with this value, for example:
# /redirect/http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291
# And in your view, use unquote_plus before the redirection:
>>> urllib.unquote_plus('http%3A%2F%2Fwww.polyvore.com%2Flords_liverpool_hey_jude_tee%2Fthing%3Fid%3D5313291')
'http://www.polyvore.com/lords_liverpool_hey_jude_tee/thing?id=5313291'
More information about Query String on Wikipedia.
You're passing in a regex, so you need to escape the special characters (ie, \?)
But if you're trying to pass in querystring parameters you'll want to handle that differently: https://stackoverflow.com/a/3711911

cannot request Django URL

l have two url patterns in my Django
url(r'^myakun', 'portal.views.myakun', name='myakun'),
url(r'^myakun/about/$', 'portal.views.myakun_about', name='myakun_about'),
when l request to www.site.com/myakun it work well, but when I request to www.site.com/myakun/about/ it will force to redirect to www.site.com/myakun
this is my views
def myakun(request):
__return render_to_response("myakun.html", locals())
def myakun_about(request):
__return HttpResponse("sdasda")
Switch order of your urls like
url(r'^myakun/about/$', 'portal.views.myakun_about', name='myakun_about'),
url(r'^myakun', 'portal.views.myakun', name='myakun'),
In your case http://www.site.com/myakun/about/ matches with first url pattern ^myakun and hence shows your that page.
Or change the url pattern to add $
url(r'^myakun/$', 'portal.views.myakun', name='myakun'),
This url:
url(r'^myakun', 'portal.views.myakun', name='myakun'),
Should be:
url(r'^myakun/$', 'portal.views.myakun', name='myakun'),
Note the EOL matching $ added in. The problem is without that '^myakun' matches '/myakun/about' and that view is returned, it never even checks to see if '^myakun/about' matches, even though it does indeed match as well.

Hyperlinks in web.py

How do I make web.py fetch a page when I click on a link?
I have this in my template:
<a href='add.html'>Home</a>
When I click on 'Home', I get 'not found'.
In my application, I have '/add' mapped to the 'Add' class which returns 'Boom!' using the template add.html.
urls = ('/', 'Index',
'/add','Add')
class Add(object):
def GET(self):
return render.add('Boom!')
I feel when I request for /add.html, the Add class will handle the request and return 'Boom!'
Why do I keep getting 'not found'?
The origin of a page URI ending in .html lilke /add.html is that in static hosting environments those were traditionally really single text files with html-content and the file ending for such a file is .html
But your system is dynamic and creates web pages on the fly. It does not necessarily need the pages ending in .html. Although you can mimic the traditional behaviour if you desire.
But in a modern and dynamic system it is often preferred that there is no ending on the single "pages" at all.
So you could, as Ignacio also suggests, just get rid of the .html and request the /add page, which is a valid and sufficient unique identifier for that resource.
If you like to keep the .html ending. you have to add it also in the route.
urls = (
'/', 'Index',
'/add.html','Add',
)
You can also have multiple routes pointing to the same resource, so that both /add and /add.html are valid and showing the same content, but content duplication has other drawbacks:
urls = (
'/', 'Index',
'/add','Add',
'/add.html','Add',
)
I recommend getting rid of the .html. This means you stick with the code from your question and create links to the page like this:
add something
Either change the route to be "/add.html" or change the link to be "add".

Pylons: How to write a custom 404 page?

So this question has been asked before, but not answered in great detail.
I want to override the default Pylons error page to make a nicer, custom one. I've got as far as overwriting the controller in error.py, as follows:
def document(self):
"""Render the error document"""
resp = request.environ.get('pylons.original_response')
content = literal(resp.body) or cgi.escape(request.GET.get('message', ''))
custom_error_template = literal("""\
# some brief HTML here
""")
page = custom_error_template % \
dict(prefix=request.environ.get('SCRIPT_NAME', ''),
code=cgi.escape(request.GET.get('code', str(resp.status_int))),
message=content)
return page
This works OK. What I'd like to do now is use a template in the templates directory, so that the 404 page can inherit my usual layout template, CSS etc.
(I know this is a bad idea for 500 errors - I'll check in error.py that the code is 404 before I use the template rather than a literal.)
So, here's the question. How do I define custom_error_template to point at my template, rather than at a literal?
You should be able to use render method (import it from yourapp.lib.base, and use return render('/path/to/error/template').
Just create your view and use add_notfound_view configuration method to configure it.
See: http://docs.pylonsproject.org/docs/pyramid/en/latest/api/config.html?highlight=document%20error#pyramid.config.Configurator.add_notfound_view

Categories