I'm trying to put my Django site in a subpath, say www.example.com/mysite/ but I can't get it to work 100%.
I read up on the answer Django Apache Redirect Problem , where it is suggested to just change the site in the admin from www.example.com to www.example.com/mysite/, which is exactly what I want to do and it almost works. All urls in the main urls.py gets redirected properly, but everything in the includes drop the "mysite" directive when using the links in the templates (one example is {% url journal_index %} which after the change should go to www.example.com/mysite/journal but goes go www.example.com/journal/).
Hope this makes sense.
Cheers!
Try adding the FORCE_SCRIPT_NAME setting:
FORCE_SCRIPT_NAME = '/mysite'
Related
I have application with DjangoRestFramework for backend and Vue.js for frontend.
My user can upload pictures and they are stored in an uploaded_media file.
Therefore I added + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to my urlpatterns.
But since it is a Single Page Application, I added re_path(r"^.*$", IndexTemplateView.as_view(), name="entry-point"), in my urlpatterns for everything to be redirected to my index.html.
My issue is since my pictures URL is fetched in this format: "http://127.0.0.1:8000/media/my_pic.jpg", it can't be rendered frontend because it's redirected to my entry-point.
So I made a 'dummy' URL path (path('media/', DummyView.as_view())) pointing to a dummy View:
class DummyView(TemplateView):
pass
And it works... just for the first picture. I am doing it right and missing something or I am going to the wrong direction?
If it helps, I find a better way than creating a 'dummy' view, I just change my entry-point URL this way:
re_path(r"^(?!media).*$", IndexTemplateView.as_view(), name="entry-point")
So everything except the URL starting with 'media' is redirected to the entry-point
I am getting a lot of search engine referral links for my previous PHP developed site that has now been migrated over to Django. I made a url redirect for the old php links like search.php?name=john+smith to the same view for my django search url as shown here:
urls.py
url(r'^search.php/$', profile_search, name='search'),
url(r'^search/$', profile_search, name='search'),
Will Google eventually update those old links if I redirect through urls.py or do I need to make a 301 redirect? If so how would I do this with django and nginx?
I would do this at nginx level - this is much more efficient than having Django handle it. Assuming the Django view expects the same query arguments, you can do this in your nginx server block:
location = /search.php {
return 301 http://$server_name/search/$is_args$args;
}
This will redirect all requests for search.php to /search/, preserving any query arguments.
A 301 response is definitely the correct approach - you don't want to serve duplicate content on different URLs.
Unless you have HttpResponseRedirect in your profile_search method, you don't actually have any sort of redirect here at. But what you really want to use is HttpResponsePermanentRedirect
def profile_search(request):
return HttpResponsePermanentRedirect('/somether/url/?based_on_request_params')
I tried this,
url ('', views.notfound, name='notfound')
But seems it doesn't work properly, for example, I have another url pattern define,
url(r'^login/$', views.login, name='login'),
So, if I go for http://example.com/login/, this works, but if i go for http://example.com/login/?help=1, then it falls into notfound category. How can I handle that?
If you just want to create a page that is displayed when a URL is not found (i.e. a Http404 exception is thrown) you can create a template with the name 404.html, and that template will be displayed any time a URL is not found.
Or if you want to define a custom 404 handler view you can define handler404 = views.notfound in your urls.py file. Then just create your notfound view, and that view will be used whenever a 404 error is thrown (and DEBUG = False)
This is a better way to catch any urls that are not recognized and display a friendly 404 page.
You can try with regex like this:
url(r'^.*', views.notfound, name='notfound'),
Make sure put it at the end of the urls.py
I think you may need to do something when url is not found, if you just need the 404 page, then the single 404.html in your templates works. And remember ti set DEBUG = False in your settings.py to see the 404 page in development environment.
Don't know what your use case is.Both above comments were equally right enough if u want to display a custom 404 page .Had u need something else to know. Check this out->
pip install django-extensions
then read this
then use the show-urls command to enlist all possible urls.
Do some manipulation or write testcases with assert to check not found urls.
Hope this is ok for your requirements or kindly comment with your suggestions cheers :-)
I am running my django app using nginx. I want to write a redirect rule such that
if user hit the url http://example.com/django/nginx/ then it redirect it to
http://example.com/django/#!/nginx/. I want o know the regex for it.
Thanks
You'll want to handle this on the client side (through Javascript, most likely), not through nginx.
From what I understand, the point of # in URLs (as per the spec) is that the portion that comes after # doesn't reach the server.
Also, see this question for some info on JS libraries for working with hash-bang urls: Are there any javascript libraries for working with hashbang/shebang (#!) urls?
Given you example I'm assuming that you are working with URLs in the form "http://1/2/3/" only, so nothing going beyond 3. Where you want to separate 2 and 3 with "/#!/". If that is the case you can try the following.
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
('^django/(?P<ajax_section>\w+)/$', redirect_to, {'url': '/django/#!/%(ajax_section)s/'}),
)
The above assumes that 2("django") in the URL will be fixed. If that is not the case you will have to try and make it a parameter as well.
I have a django app running on http://djangohost/appaddress. I'd like the project to be available at http://differentaddress/app . Currently I'm able to run app at the desired address but using {% url %} templatetags gives me improper address in the form http://differentaddress/app/appaddress. Also when I go to django app address directly all {% url %} links are in the form http://djangohost/app/appadress How can I change this ? I have these entrances in apache conf :
ProxyPass /app/ http://djangohost/appaddress/
ProxyPassReverse /app/ http://djangohost/appaddress/
You'll probably have to tell Django where it is running by manipulating SCRIPT_NAME
http://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#force-script-name
Or, if you want to keep things within Apache, you may give a try to mod_proxy_html - disclaimer: haven't used it myself, but it does claim to rewrite links in HTML pages
Maybe not a proper solution but still some workaround for the problem without interfering with apache's settings. Tested with mod_msgi and it works like a charm. Here's the link :
http://fromzerotocodehero.blogspot.com/2011/01/using-proxypass-with-django-project.html . Basically I've overriden built in url function here creating custom urlc temlpatetag. In tag's code I've added line replacing first occurence of unwanted app name with empty sign.
So you want to "mount" a Django site on a sub URL path? I already tried this with Apache and mod_proxy, and it was kind of a nightmare to find out. Here's what I have come up with (probably not complete or perfect):
# In your scenario
FORCE_SCRIPT_NAME = "/app/"
# End of settings
_prefix = (FORCE_SCRIPT_NAME or "")
LOGIN_URL = _prefix + LOGIN_URL
LOGIN_REDIRECT_URL = _prefix + LOGIN_REDIRECT_URL
LOGOUT_URL = _prefix + LOGOUT_URL
ADMIN_MEDIA_PREFIX = _prefix + ADMIN_MEDIA_PREFIX
Obviously, this prepends "/app/" to the most important hardcoded site URLs, plus it sets FORCE_SCRIPT_NAME to ensure that {% url something %} will result in an absolute URL of "/app/something", for instance.
This worked for me using mod_wsgi for the Django site and ProxyPass/ProxyPassReverse for the "mounting". Try it out and give me feedback, I'm interested whether this is a general solution.