Django not reflecting changes to urls.py - python

I am setting up a login system for a Django site, which is running on an Nginx server. I'm getting the following debug 404 page:
Using the URLconf defined in it.urls, Django tried these URL patterns, in this order:
^admin/
^login/ [name='main_login']
^$ [name='index']
^laptops/
^assets/
^mardes/
^users/
^jobs/
^static\/(?P<path>.*)$
The current URL, account/login/, didn't match any of these.
This appears to be using an old version of the it.urls file; the current one looks like this:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls) ),
url(r'^account/', include('account.urls', namespace='account'), name='account' ),
url(r'^laptops/', include('laptops.urls', namespace='laptops') ),
url(r'^assets/', include('assets.urls', namespace='assets') ),
url(r'^mardes/', include('mardes.urls', namespace='mardes') ),
url(r'^users/', include('users.urls', namespace='users') ),
url(r'^jobs/', include('jobs.urls', namespace='jobs') ),
url(r'^', TemplateView.as_view(template_name='it/index.html'), name='index' ),
) + staticfiles_urlpatterns()
Which, as you can see, has no r'^login/' pattern.
I have set LOGIN_URL to '/account/login/' in my settings.py file, however the #login_required decorator is loading '/login/'. The new url is also ignored when I specify login_url='/account/login' in the #login_required call.
I have restarted Nginx (both by starting and stopping, and running 'restart'), this has made no different. I have also set 'sendfile=off' in the nginx.conf file. I am guessing there is cache stored somewhere (re: How to clear the cache of nginx?).
Please let me know if you need more details.

Nginx doesn't run Django alone, but it should interact with a wsgi app server like uwsgi or gunicorn.
Maybe you should restart the wsgi app server instead of nginx.

good catch #bcvery1. Also for those who are running django apps via passenger, restarting nginx or re-creating restart.txt file should also work to see those changes.

Related

How to serve django and gatsby on single vhost?

I'm trying to deploy gatsby based frontend with django based backend under a single domain. It will rely on Apache and mod_wsgi. In a perfect world it should work as following:
https://my-domain.com/ - gatsby frontend
https://my-domain.com/admin - django
https://my-domain.com/api - django
I can see two possibilities:
Django is aware of frontend. Serve everything via Django, setup / as a STATIC_URL.
Django is not aware of frontend. Serve /api and /admin via django. / is handled by a webserver.
I feel more comfortable with the second approach, however I do not know how to configure VirtualHost for such a scenario. The firstch approach looks like an ugly hack.
How should I proceed with that?
After compiling your gatsby project, it should be served by django as a static page.
First: The gatsby dist should be in your static_private path.
Second: In your django project, you will define a URL for / that will call an index view let's say.
Finally: in your view you should render index.html of your gatsby dist.
urls.py:
from django.contrib import admin
from django.urls import path, re_path, include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('apis/', include('apps.urls')),
path('/', views.index),
]
views.py:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
Note that if you are handling routing in your frontend your url pattern for the index view should be like this : re_path('^.*$', views.index)
If you are hosting your django app on heroku you will need the whitenoise middleware and set it up in your settings.py :
MIDDLEWARE = [
...
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]
A doc is available here: https://devcenter.heroku.com/articles/django-assets#whitenoise

Django backend not returning JSON on production server, getting 404

I have a Django backend that returns json data. I'm able to get data back on my localhost but got a 404 on production server. I'm running nginx in front of gunicorn server. Any ideas why I'm getting a 404? Shouldn't this be able to work to retrieve json data, or do I need to use django rest framework and implement viewsets to make this work?
Not Found
The requested URL /about was not found on this server.
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^about', about.get_info),
]
about.py
from django.http import JsonResponse
def get_info(req):
return JsonResponse({"test": "hello"})
The problem is inside url.py. The way the rules are defined currently, it would only allow you to open about/ and admin/, i.e. with the / at the end. To fix this, you can define the URLs as following:
urlpatterns = [
url(r'^admin/$', admin.site.urls),
url(r'^about/$', about.get_info),
]
Now you should be able to use both admin/ and admin to access the page.

Trouble loading Django App after testing other Django App for several days? Always redirects to requested URL that doesn't exist?

Over the weekend I attended a hackathon, at which I created a Django app that ran on 127.0.0.1:8000/myapp/list on my local machine with
python manage.py runserver
Today, I tried to launch another Django app that I had made in the past for bug fixes and things, but when I ran manage.py runserver it keeps redirecting me to 127.0.0.1:8000/myapp/list, even though it's a totally separate app.
Anyone know how to resolve this issue?
urls.py for Django app that I'm trying to run:
from django.conf.urls import url
from TweeTest import views
urlpatterns = [
url(r'^$', views.view_home, name='home'),
url(r'^about/$', views.AboutPageView.as_view()),
url(r'^contact/$', views.ContactPageView.as_view()),
url(r'^result/$', views.view_tweet, name = 'result'),
]
urls.py for Django app I made over the weekend:
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myproject.myapp.urls')),
url(r'^$', RedirectView.as_view(url='/myapp/list/', permanent=True)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Is it something to do with the RedirectView line in the second urls.py?
I'm not sure why a totally different Django app is trying to connect to the same URL as the other one... seems strange.
If someone knows the answer to this, that would be greatly appreciated!
Thanks!
UPDATE:
When I try to connect to 127.0.0.1:8000 on incognito mode, it works fine. Could this mean it's a browser issue?
Davis
permanent=True sends a HTTP 301 Permanent redirect to the browser. Browsers tend to cache 301 responses very aggressively, meaning your web browser currently remembers that http://127.0.0.1:8000/ is a permanent redirect to http://127.0.0.1:8000/myapp/list/. Since your browser doesn't know that different Django apps might be running on 127.0.0.1:8000 at different times, it always follows the cached redirect.
You shouldn't use permanent=True unless you know what you are doing. While it can boost the performance on a high-traffic site, it is rarely necessary for low- to medium-traffic sites, and the permanent caching can be a real pain for developers.
Either clear your browser cache, run your Django test server on a different port, or use a different browser.
Have you tried using a different browser or clearing your browser history? This sounds like a browser issue to me.

Django URLconf error

I am trying to build a website with Django. I am new to Django and have followed the Tango with Django tutorial.
I keep getting an URlconf error I do not understand.
I have a domain (www.example.com), an app (mainApp) and two views in mainApp (homePage, registration).
I want
http://www.example.com to be matched with the homePage view
http://www.example.com/registration/ to be matched with the registration view.
My urls.py file for the project is
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns(
'',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('mainApp.urls')),
)
The urls.py file for mainApp is
from django.conf.urls import patterns, url
from mainApp import views
urlpatterns = patterns(
'',
url(r'^$', views.homePage, name='homePage'),
url(r'^registration/$', views.registration, name='registration'),
)
This configuration displays the homePage view correctly, but not the registration view. The error is:
Using the URLconf defined in myProject.urls, Django tried these URL
patterns, in this order:
^admin/
^$
^/
The current URL, registration/, didn't match any of these.
What causes the error?
It looks fine. If you're hosting it online you should restart the webapp, if hosting it locally then restart the local host. Without restarting it the changes don't register.

Django - How to use URLconfs with an apps folder?

I'm following the tutorial on the Django website but I'm trying to expand upon it. I like the organizational scheme of putting all of your apps in an "apps" folder. I'm trying to figure out the proper way to include urls.py in order to get everything to link together.
Here's my root urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('apps.polls.urls')),
(r'^admin/', include(admin.site.urls)),
)
Here's my urls.py at apps/polls/urls.py:
from django.conf.urls.defaults import *
urlpatterns=patterns('polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
)
What's the correct way to do this? Thanks!
The way you currently have it set up... the URLs for polls would be:
http://your.url.here/polls/polls/235/results/
This is probably not what you want. The include function in the urlpatterns in the root urls.py file specifies "polls/" as a prefix to all urlpatterns in the polls app. Therefore, in the polls/urls.py file, you shouldn't specify the "polls/" prefix again as it will cause duplicate prefixes.
How are you running your Django instances? If you have multiple vhosts configured in Apache then each Django instance in /apps has it's own urls.py.
I got it to work by doing this:
urlpatterns=patterns('polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
I guess the polls part is taken care of in the root urlconf

Categories

Resources