Django wrong url pattern for subpath - django

I don't really know how to explain well my issue.
What I'm trying to do is the following.
My admins linked my Django server address 192.168.2.1:8001 to a web address: devel.genesilico.pl/modomics to make public the website that otherwise would be visible only on our local network.
If I visit that address I see the main page of my site without the static files, and if I try to visit another page the address changes to:
devel.genesilico.pl/modifications
So the modomics prefix path is removed and it cannot find the page.
I thought that changing my URLs.py adding modomics in front of all of them would resolve this issue, but no. At this point, the DEBUG of Django shows me this (photo attached).And also in this case the prefix "modomics" is removed.
I don't know what to do. I don't know if this problem is given by Django or by the setup of the web address that my admins gave me.
Note that I'm testing this only for the app Modifications.
Here my main urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('core.urls')),
url(r'^', include('modifications.urls')),
url(r'^', include('sequences.urls')),
url(r'^', include('pathways.urls')),]
And here the urls.py for the app modifications:
urlpatterns = [
url(r'^modomics/modifications$', views.modlist, name='modlist'),
url(r'^modomics/modifications/(?P<mod_name>.+)/$', views.moddescription, name='moddescription'),
]

It looks like you need to configure your application to run on a sub-folder.
What's happening is that the reverse function is producing relative URLs to a root directory, /, instead of /modomics because your server isn't aware it's being hosted differently.
There are two ways to solve this:
In settings.py, you can set FORCE_SCRIPT_NAME = '/modomics' to get the URLs to generate properly, or
have your admins pass /modomics as the value of a header X-Script-Name and process it in wsgi.py - see this related answer
The drawback to the first solution is that you need to make sure that your website is configured to run differently in development and prod by using one of the settings file patterns for deployment, and you need to coordinate with your admins if the subfolder ever changes.

Related

Django URL to Template View Preventing Media Folder from Displaying

I have been working on a Django DRF/Vue project and ran into issues with being able to download or view files from the media folder. I have this set up as a media root and directory in the settings and included the following in my url.py file:
if settings.DEBUG:
urlpatterns += static(
settings.STATIC_URL,
document_root = settings.STATIC_ROOT
)
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)
The issue is happening from my path to the template views.
re_path(r"^.*$", IndexTemplateView.as_view(), name="entry-point")
If I comment this line out, the media directory works as I want but this causes my application to no longer direct users to my frontend and Django template files. I am fairly new to Django and am not sure exactly how to resolve this, or if it is possible to create a condition within the path to exclude /media from being triggered?
Edit: I was able to make this somewhat work by changing re_path(r"^.*$", IndexTemplateView.as_view(), name="entry-point") to path('', IndexTemplateView.as_view(), name="entry-point"). My app currently runs Django on port 8000 and Vue app on port 8080. I access the app front end from 8080, which allows it to re-direct to the login page if necessary. If I try to refresh any page with this updated config, I am faced with the error, "Using the URLconf defined in app.urls, Django tried these URL patterns, in this order:"... and it shows a list of my api URLs. I find URL patterns with Django one of the most confusing things about trying to learn this framework. Can someone please explain to me what is causing this?
I was stuck on this for a long time and finally figured it out. Of course it was a really simple fix. All I had to do, after changing the 're_path' to 'path' was change the Vue route type to hash instead of history.

django project derived from python-socketio django example unable to server other URLs (at least in django 1.11)

After playing with python-socketios django_example, and seeing that it worked great, I created a new django project, configured it just like the example, copied over the example app to the project (complete with it's overriding of the runserver management command). Everything worked fine, and I was able to make my a few changes so that you can set a nick, some redis stuff to lookup up the sid for a nick, and was able to support sending private messages to a nick. Everything was still working great.
I figured the next logical step was to, rather than having to manually set a nick, require the user to login, expose their username as a var in a script block in the template (I moved scripts/index.html to templates/index.html), and automatically have the javascript emit my custom 'set_nick' event with the username automatically upon connect.
I defined LOGIN_URL = '/accounts/login' in settings.py, included 'django.contrib.auth.urls' in my urls.py and wrapped the index view with #login_required.
It was only then that I noticed that no matter what URL you request, you always get the chat apps index view - no login page redirect, '/admin/' is ignored, etc.
EDIT Solved - See my answer below.
I noticed that the urls.py which I blindly copied over from the example looked like this:
url(r'', include('socketio_app.urls')),
url(r'^admin/', admin.site.urls),
and the r'' was the culprit (matches everthing). Changing this to:
url(r'^/', include('socketio_app.urls)),
url(r'^admin/', admin.site.urls),
However, I'm using Django==1.11. I believe Django 2 tends to suggest using path (or some similarly named function) rather than using url. I don't believe, however, that the semantics of url are different in Django 2, so this is probably an issue for Django 2 users as well.

Django default root page stops working when I add my app to urlpatterns in urls.py

I am new to Django and only followed one tutorial on Visual Studio which worked fine.
Now I was trying to create another project on PyCharm and am facing the below problem.
the default Django template sets up fine in Pycharm and I see the default root/landing page when I run the project.
but the moment I add a url for my app in urls.py the default Django page stops working, although both the admin url and myapp urls work fine.
my Urlspatterns is provided below:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^myapp/', include('myapp.urls', namespace='myapp')),
]
the moment I remove the line the default Django landing page starts working again.
I have tried searching online but was unable to find an explanation. Posting it here because I would really like to know why this happens.
This page is intended only to show that django is working properly. As soon as you add any url it is disabled.

Best practices for path-agnostic links in Django?

What is the best way to code html links in a Django application
which is intended to be distributed to other users and
where it cannot be known in advance what the final
URL path to the application will be?
Here are some links I am currently using:
<li>By Date</li>
<li>Trends</li>
This works fine when the app is configured like this in mysite/urls.py to
be in the root path.
url(r'^', include('myapp.urls')),
But if you change mysite/urls.py to run the app in a different path:
url(r'^myapp/', include('myapp.urls')),
then the links break. This seems like it ought to be a common scenario but I have been unable to discover how to solve it cleanly.
You want url tag:
Returns an absolute path reference (a URL without the domain name) matching a given view function and optional parameters. This is a way to output links without violating the DRY principle by having to hard-code URLs in your templates

simplest way to combine two Django projects

Best case scenario: Just modify the urlpatterns of one of them to include the urlpatterns of the other.
But as of now they both have seperate settings.py, seperate DB's, seperate directories. I assume I may have to somehow merge their two settings.py, include one of them in the other's INSTALLED_APPS, and resolve a bunch of directory issues. Can I somehow just have one invoke the other through urls.py and forego all the above. Any website documentation covering all this in detail, that's the main thing. Sorry if this has been asked. The problem is the existing Django project is running under one uwsgi process on the server, and adding another uwsgi process bumps it up to another account level.
Not sure if this is what you mean by "invoke the other through URLs.py", but you can include URLs from one app within another very easily. For example:
urlpatterns = patterns('', url(r'^polls/', include('polls.urls')))
That will include all the URLs in the polls app. You can read more on it here:
https://docs.djangoproject.com/en/dev/intro/tutorial03/#decoupling-the-urlconfs
This is state of my knowledge now. Something like uwsgi and I guess other server schemes ask for the specification of a single settings.py as a parameter. So as far as integrating multiple Django projects in a single uwsgi process, they will have to share a single settings.py. The only problem there is only one setting for MEDIA_URL and MEDIA_ROOT. And the only solution I see is putting the media from both projects in one folder - seems unreasonable. Am I missing something.