Create a 'dummy' URL path for frontend pictures rendering - django

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

Related

How do i configure django urls to make it so if it displays 404, it reroutes to Reactjs

The title is terrible - sorry about that.
So say I have a website say www.myhairynose.com/. I've configured my urls.py so that whenever the person enters www.myhairynose.com it redirects them to the index.html (where it displays the reactjs application). I've set reactjs with react-router and made it so when you click a button on the index page it goes to www.myhairynose.com/#/webpage.
Thing is... I don't want that.
I want it to go to www.myhairnose.com/webpage. I want react to handle this however and not django. ALL of this url thing should be in the same application of index.html. How do I configure django urls.py so that if I enter www.myhairynose.com/webpage it goes to the index of www.myhairnose.com ReactJs's application and then it checks to see if the route has webpage. Otherwise say if I went to www.myhairynose.com/asdkjaskldsaj (which the react route doesn't have) it should display a 404.
______TLDR______
Basically what I want to happen:
User goes to website.com/name
>if website.com/name exists in django - it returns a html doc
>if not then it redirects to website.com/ where ReactJs will handle the url and see if it matches any of the routers
...> if it does, then it displays that route
...> if not then it displays a django 404 or a custom 404 page.
How do I configure django and reactjs to do this?
Are you using Django to render react? I am making the assumption that you have some view like views.LoadReactView.
What you need is a catch all route.
from django.contrib import admin
from myapp import views
urlpatterns = [
...
url(r'^admin', admin.site.urls),
url(r'^', views.LoadReactView.as_view()),
]
This should appear as your very last pattern. You essentially are saying "after checking all the other routes, if there are no matches, go here".
In addition, you could also actually just define your main view as your 404 handler
urlpatterns = [
...
]
handler404 = views.LoadReactView.as_view

Running Django site with a URL prefix

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'

Django: Making alias in urls.py for amazon s3?

I am encountering a problem in setting up the URLs in Django.
To serve my media files I have this Amazon S3 bucket:
https://somebucket.s3.amazonaws.com/
I set the media URL inside settings.py as follows:
MEDIA_URL = https://somebucket.s3.amazonaws.com/media/
Inside the urls.py I set the code as follows:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_URL}));
My intention is that, when I point an image file from, lets say <img src="/media/image.jpg"/> it will be automatically pointed to http://somebucket.s3.amazonaws.com/media/image.jpg
How can I do that? I have tried many methods but it always returns a 404.
However if i try to access the file directly http://somebucket.s3.amazonaws.com/media/image.jpg it works.
Why would you want to do that? That defeats most of the purpose of having the external storage in the first place. It means that for every media request, it has to go through Django itself, to resolve the URL and generate the redirect to S3, with all the overhead that implies.
Instead, as sneawo suggests in the comments, you should simply set the img src attribute to point to the image via the S3 URL.
turned out that django automatically appends the media_url in front of the imagefield url.
i was under impression that i have to append the media_url, which caused me to look for a simpler solution.
Django Admin only shows relative paths
![django admin shows relative paths][1]
http://i.stack.imgur.com/aZGPy.png
But, tastypie gives me absolute path
http://i.stack.imgur.com/SEdaX.png
so i now don't have to worry about urls anymore...thanks again guys :)

Django-CMS AppHooks with conflicting urls?

I'm trying to use django-cms app hooks in a different way. I have only an app, with different website pages. For each page, i created an AppHook, since i want to have control of all of them with the cms.
To do that, inside the app, i did a package, with urls.py file for each of the page, example:
/urls
/home_urls.py
/portfolio_urls.py
/contacts_urls.py
Here are the definition of some app hooks:
class WebsiteHome(CMSApp):
name = _("cms-home")
urls = ["website.urls.home_urls"]
apphook_pool.register(WebsiteHome)
class WebsiteServices(CMSApp):
name = _("cms-services")
urls = ["website.urls.services_urls"]
apphook_pool.register(WebsiteServices)
Anyway, the problem is: i don't have any control on the regular expressions. Each one, is entering on the first regular expression that it founds, in this case, the urlpattern in the
website.urls.home_urls
Despite, having different apphHooks.
Example:
if i write a slug contacts (that has an apphook to WebsiteContacts), it still goes to the home_urls.py file, associated with the WebsiteHome (app hook).
Did anyone had a similiar problem?
Basically, what I'm trying to say is that it's something wrong with the regular expression. I can't make:
url(r'^$', [...]),
only:
url(r'^', [...]),
If I put the '$', it doesn't enter on any regex. If I take it, it enters always on the
website.urls.home_urls.py
Despite the slugs having different Apphooks, associated with different urls.py files.
Have you tried r'^/$'? I'm using r'^/?$' in some app-hook urls, but I wonder if r'^$' is failing for you because of a '/'?
As you've defined each of those URL files as individual app hooks in CMS then they'll each get attached to a certain page in the CMS e.g.
www.mysite.com/home
www.mysite.com/contacts
www.mysite.com/services
etc
Because those URL files are attached to pages this should prevent conflict between urlpatterns. For example, I've got an URLs file attached to a CMS app called News which looks like this;
urlpatterns = patterns(
'',
url(r'^(?P<slug>[-_\w]+)/$', NewsDetailView.as_view(), name='news_detail'),
url(r'^$', NewsListView.as_view(), name='news_list'),
)
Which is attached to a page at mysite.com/news so if I go to mysite.com/news/myslug I hit that NewsDetailView and if I go to mysite.com/news I hit NewListView.
Using this example, if you had a slug for a contact you'd go to mysite.com/contacts/contact-slug to hit that NewsDetailView.
And just a sidenote on the urlpatterns in case you're not aware, the ^ in the regex signifies the start of a pattern to match, and the $ signifies the end. URL dispatcher docs

Url mismatch, django template link

I've got a prob with a link in the sidebar of my django site, in the template it's like that:
<li>Profile</li>
while in the urls.py:
url(r'^(?P<user_id>\d+)/profile/$', 'auth.views.show_profile', name='profile')
When i access it from the main page with url: e.g /1001/profile/ it loads fine but when I try to access it from another subpage with url: e.g /1001/forms/profile/ i get the error: The current URL, /1001/forms/profile/, didn't match any of these. How can i fix this?
It is because "profile" is a relative URL, and a relative URL is appended to the current URL - the resulting address is not valid across the whole site. Seems like you should use an absolute URL in your case.
At the template you can try something like:
Profile
UPDATE
To get request available in templates you have to add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS. I'm not sure if it is added by default.
You must have to add your second subpage url in urls like you did for /1001/profile/
url(r'^(?P<user_id>\d+)/form/profile/$', 'auth.views.show_profile', name='profile_form')
and also correct your code as #Paulo mentioned or you can also do it through reverse url.
Profile