I am looking for how to implement URL rewrite in Django which is explained in http://en.wikipedia.org/wiki/Rewrite_engine .
For example,I would like to convert
http://127.0.0.1:8000/employer/full_detail/20/
to
http://127.0.0.1:8000/employername-20.htm
How can I implement this in Django? could suggest me a document or give an example ? Thanks
Thanks
You don't want to rewrite urls through django (really...you probably don't want to rewrite them at all) You might be looking for a redirect, or you might want to rewrite/redirect from whatever you're using to serve Django in production (nginx or Apache most likely)
Related
I want to configure my urls something like these:
username1.mysite.com
username2.mysite.com
You know, the parameter is actually the username, and its placed before the domain name. Other sites do this I have seen, for example, Wordpress Blogs and so on. Can I do this using Django URLs? Any help is appreciated.
I cannot think of a way to do this using Django's URLs because this is a subdomain NOT a URL parameter. What I would suggest is reading the subdomain in your server config (e.g nginx site config) and writing it into a header to be read by some custome middleware in django
I accomplished something like this with the "django-dynamicsites" plugin.
https://bitbucket.org/uysrc/django-dynamicsites
It's quite old and may need a few tweaks for modern Djangos but it does do what you need, I use it myself and it works well.
It involves modifying the django_sites table so doesn't play too nice with makemigrations. You may have to use the SQL to update the SQL database on your server to get around that.
I stumbled up django-subdomains which does exactly what I want. Thanks everyone.
I've been trying out Ruby on Rails and really like the RESTful approach for the URL's.
Now I'm trying to learn Django and I want to create the same kind of nested URL's as I did with Rails.
Lets say I wanna do this: /categories/12/products/13
This is what I've came up with, but does not seem to work:
url(r'^categories/(?P<category_id>\d+)/products/(?P<product_id>\d+/$)', 'my_app.views.product', name="product"),
How should this be done? Also, how could you use the url template helper for creating this kind of link?
Check out TastyPie. It's a drop-in REST Api for Django similar to what you're used to in Rails and will get you going in the right direction without too much work.
Glad you got it sorted, as TastyPie was mentioned I would also recommend Django REST framework also.
In a template you can use Link title
To create a url in your python code you can use reverse:
from django.core.urlresolvers import reverse
url = reverse('product', args=[category_id, product_id])
For example, I have a django project, there are two pages.
http://domain.com/A/things
http://domain.com/B/things
Now I want that visitors can visits these pages with url
http://A.domain.com/things
http://B.domain.com/things
I tried to use nginx to rewrite the rule. Such as rewrite "http://A.domain.com/things" to "http://domain.com/A/things".
But when I use the function reverse or the templatetag url, the url will still be "/A/things" instead of "http://A.domain.com/things"
As we know, django dispatch visits to views based on urls. I wonder if there is a way to make django dispatch visits based on domain?
Look at django-hosts app. It provides very convenient work with subdomains.
https://github.com/ennio/django-hosts
You could have different Django projects in differente VirtualHosts, another option would be to use the Sites Framework
I am farily new to the topic, but I am trying to combine both Django and Pyjamas. What would be the smart way to combine the two? I am not asking about communication, but rather about the logical part.
Should I just put all the Pyjamas generated JS in the base of the domain, say http://www.mysite.com/something and setup Django on a subdirectory, or even subdomain, so all the JSON calls will go for http://something.mysite.com/something ?
As far as I understand now in such combination theres not much point to create views in Django?
Is there some solution for clean urls in Pyjamas, or that should be solved on some other level? How? Is it a standard way to pass some arguments as GET parameteres in a clean url while calling a Pyjamas generated JS?
You should take a look at the good Django With Pyjamas Howto.
I've managed to get the following to work, but it's not ideal. Full disclosure: I haven't figured out how to use the django's template system to get stuff into the pyjamas UI elements, and I have not confirmed that this setup works with django's authentication system. The only thing I've confirmed is that this gets the pyjamas-generated page to show up. Here's what I did.
Put the main .html file generated by pyjamas in django's "templates" directory and serve it from your project the way you'd serve any other template.
Put everything else in django's "static" files directory.
Make the following changes to the main .html file generated by pyjamas: in the head section find the meta element with name="pygwt:module" and change the content="..." attribute to content="/static/..." where "/static/" is the static page URL path you've configured in django; in the body section find the script element with src="bootstrap.js" and replace the attribute with src="/static/bootstrap.js".
You need to make these edits manually each time you regenerate the files with pyjamas. There appears to be no way to tell pyjamas to use a specific URL prefix when generating together its output. Oh well, pyjamas' coolness makes up for a lot.
acid, I'm not sure this is as much an answer as you would hope but I've been looking for the same answers as you have.
As far as I can see the most practical way to do it is with an Apache server serving Pyjamas output and Django being used as simply a service API for JSONrpc calls and such.
On a side note I am starting to wonder if Django is even the best option for this considering using it simply for this feature is not utilizing most of it's functionality.
The issue so far as I have found with using Django to serve Pyjamas output as Django Views/Templates is that Pyjamas loads as such
Main html page loads "bootstrap.js" and depending on the browser used bootstrap.js will load the appropriate app page. Even if you appropriately setup the static file links using the Django templating language to reference and load "bootstrap.js", I can't seem to do the same for bootstrap.js referencing each individual app page.
This leaves me sad since I do so love the "cruftless URLS" feature of Django.
That is to say, let's say I'm writing something that's hosted on foo.com. I'd like it to be possible for a user who goes to foo.com/bar.com to be served up bar.com from foo.com and to be able to interact with bar.com (e.g. navigate to foo.com/bar.com/baz via point-and-click). I understand that this is what a proxy is supposed to do. I need to do some preprocessing of a request to access the proxy, which is why I'm turning to a web framework. I've a preference for django, rails, or sinatra, or another python/ruby solution, but any will do, really.
Thanks in advance; alternate suggestions are welcome.
First you will need to parse the URL at foo.com. In django you could have an url like this(not tested):
url(r'(?P<url>.*)$', my_proxy_view, name = 'proxy')
So http://foo.com/bar.com/baz/ will give you an url of 'bar.com/baz/' you may use as you please in your view.
Then you have to retrieve the page at bar.com, using a library like urllib2.
When you have the contents of the remote page, you need to change all links(anchor elements) that point to bar.com to point to the URLs of your proxy. If you want to proxy images, stylesheets and javascript you need to change the links of those as well.
You probably want to cache as much as possible as well. And be sure to set a user-agent on the urllib request that will let the other site know that this is some kind of robot or proxy.
With that said, this sounds like a really stupid idea. What is your use case?
i can only talk about django, but....
if you only want to use the same object/data on multiple websites you should have a look at the django sites framework
for redirects i would suggest the redirects app
or you simply use the redirect shortcut in your views