Django: detecting request domain in urls.py - django

I have a Django app that will serve diferent websites.
Each one will have it´s own domain
Each one will have it´s own sub app with templates and views
They all share the same backend, models and data
My aproach
As I already have the database with the segmentation info I need to show the desired products in each site and I will have different views for each sub app, I don´t need to add another field in the models.
I think that it would be easier just to detect the request domain in my main app urls.py and rout home url to the desired sub app.
Something like:
# urls.py
if "name1" in request.domain:
urlpatterns += [path('', include('app1.urls'))]
if "name2" in request.domain:
urlpatterns += [path('', include('app2.urls'))]
else:
urlpatterns += [path('', include('app3.urls'))]
Maybe I should make a middleware and set a global variable I can access in urls.py? Can I use this kind of if statements in urls.py?
Django sites
I checked the Django Sites Framework, but if I understand ir well, it´s more focused in segmenting the database in the models, not the templates and views. In any case I don´t really catch how the Sites framework detects the incomming URL and roots the request to each sub app.
Other intents
I searched for more info and this article can brief the different articles about the issue I found.
https://medium.com/crowdbotics/how-to-use-dynamic-subdomains-in-django-dc1cb2cac00b
But still I don´t get how can I achieve what I need. Sounds very logical just to use my aproach and don´t mess with my database. If it´s possible.
Any clues welcome. Thanks in advance!

Related

The default django admin causes lot of problems

As I'm new to Django so I'm going to ask a stupid question on how to use Django features of session , cookies without using default django admin , I mean deleting it and creating everything new.
Any help or suggestion will be appreciated!
Well in Django the admin panel is very customizable you can just read the documents about how to customize your Admin panel. you just need to import CustomUsers in the models
here is the link. And admin panel of Django is great so it is always recommendable but if you still don't want it then just remove in from the main URL.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('mainprocess.urls')),
]
just remove the admin path and add your self-made path.
Django admin is just a tool which is not required for any other features by any mean.
So you just need to search about topics you said you want to have in youtube and find videos about it and learn!
my suggestion is to not learn anything related to html and templates and forms in django because it will not be used in your job in company in feature and focusing on creating API and Authentication and writing models on django

How can I find the view mapped to certain url in django with pycharm?

I'm debugging multiple apis built with django rest framework , How can I find the views mapped to a certain URL?. I'm currenty using pycharm so is there any plugin than can help
EX:
in url : /api/users/business-segment/
how can I know the view that maps to this url ?
I agree there could be a more automatised way, but I look in urls.py files starting from the left. So the urls.py file from the users app will guide me to business-segment, which is most likely a ViewSet.

What is the best practice method of using Angular JS routing with a Django backend?

I have created a REST API in Django to pull data from my database. I have a front end application built with Angular that makes calls to that same API. The API has a few URL's, and I have one other URL to serve up index.html and handle the routing. Angular injects the "#/" into the URL. Ideally I would not have that, but when I use the HTML5 mode and location provider, Django picks up the URL and does not see the specified URL in its list and therefore throws an error.
I have seen some resources online, but they are not very clear to me.
Basically, what are accepted best practices with regard to creating angular applications with a Django backend.
I appreciate your help! Thank you in advance.
If you want to serve the index.html for every url and then do the routing in angular you can do somethings like this in your <project_folder>.urls.py
from <your_app> import views
urlpatterns = patterns('',
url(r'^.*$', views.index),
)

How are Django page templates assigned to each page?

I couldn't find this info in the Django docs, but I'm sure it is there, I'm just very new and don't know what terms/etc to search on.
How are Django page templates assigned to each page?
I have a login to a Django site, and also SFTP access to the site. I don't think my Django login is a superuser/full-admin though because the interface seems pretty limited compared to other CMS systems. I can edit pages, posts and the media library, but I don't see anything that says how each page is assigned a template.
For example, I have this file /mysite/templates/pages/index.html
I know that template is being used for the home page because it has all of the content that is specific to the home page on it, and changes I make show up on the home page.
I tried copying that file to test.html, but when I browse to test.html in my browser, I get a 404 error (I also get that error if I go to index.html). So there must be something else that maps a template to a page, but I'll be dambed if I can find it. Will I need more access to the admin area, or can I do something with SFTP? I also have SSH access but wasn't able to follow any of the steps online to create a new superuser account for me, for Django.
Edit: Thanks for both answers, after I work through this I'll accept whichever helped the most. I do not have a views.py file, but I think it might be using an extra module for this routing, I have this in my urls.py file:
urlpatterns = patterns("",
("^admin/", include(admin.site.urls)),
url("^$", "mezzanine.pages.views.page", {"slug": "/"}, name="home"),
("^", include("mezzanine.urls")),
)
Is this "mezzanine" something different which changes the answer (location of views.py or list of views)?
url.py is the file that maps the urls to methods that return rendered templates. In essence you define the url and a method and when someone goes to that url, that method gets called which returns a HTTP response with the rendered template. This map is called urlpatterns. In the following example when someone goes to yourwebsite/blog then in the blog apps, view.py, page method is called, which will use a template and render that with specific information.
urlpatterns = patterns('',
url(r'^blog/$', 'blog.views.page'),
url(r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
)
Have a look at this link.
https://docs.djangoproject.com/en/dev/topics/http/urls/
Django uses urls.py files to map paths to views. This match is resolved using regular expressions. When a match is found, Django executes the associated view (usually inside views.py). The view is in charge to render the template required for the path (by finding it on the server's hard disk and loading it).
All aforementioned means that there's no direct association between a url path (i.e www.example.com/path/to/page) and a file on the server's hard disk (i.e /server/path/to/page). It's all performed dynamically by Django's engine when a request comes in.
If you want to know which view is gonna be generated for a specific path, follow the regexs at urls.py until you find the path you're looking for. Then open the view for that url and see inside which template it is rendering.
Reading doc's URL Dispatcher is a good point to start learning about this.
Hope this helps!

Integrating Sphinx and Django in order to require users to log in to see the documentation

I am curious if it is possible to hide sphinx documentation inside a django app so that only people who log in can see it. It seems to me that since sphinx creates its own structure and that Django uses the urlconf to define which pages a user can see, that it wouldn't be possible to combine the two. Although there must be some combining since the Django website likely uses django and sphinx. I am wondering if anyone has any insight or if they can point me in the right direction.
Thank You in Advance!
Sphinx builds your docs into HTML files, so in most cases this docs should be served by your web server rather then Django. However Django is able to serve static files as well.
You can use the django.views.static.serve function to do this and wrap this function with login_required. E.g:
from django.views.static import serve
from django.contrib.auth.decorators import login_required
urlpatterns += patterns('',
url(r'^docs/(?P<path>.*)', login_required(serve), {'document_root': '/path/to/sphinx/build/html'}, 'docs'),
)
However this configuration will be considered a bad practice in production environment as in this case Django will serve both html and css/js files from your sphinx theme.
The first improvement you can do here is to serve /path/to/sphinx/build/html/_static/ with apache/nginx or whatever you use.
The more proper way is to serve docs with apache/nginx and make it handle the auth itself. Unfortunately I made a quick Google search but did not find a way to use Django's User model to handle http_auth in apache or other. Alternatively you can use something like mod_sendfile or X-Accel modules - http://www.wellfireinteractive.com/blog/nginx-django-x-accel-redirects/ In a nutshell - Django app checks permission if user can view the file and add special header to response containing file path. Webserver will serve this file instead of original message from django