New project in virtual env referencing another project's default/redirected path

104 views
Skip to first unread message

Doug Nintzel

unread,
Jan 21, 2018, 5:13:21 PM1/21/18
to Django users
Hello,

I am new to Django and followed this Mozilla Django Tutorial which was very helpful, and created the 'locallibrary' project.
As part of the exercise, it has you create a 'catalog' app and has you set up a redirect to the default app ('catalog') as below

locallibrary\locallibrary\urls.py
     path('', RedirectView.as_view(url='/catalog/', permanent=True)),


The whole tutorial went smoothly, but now I am wanting to create my own project so I created a new virtual environment, created a new site/project, and for sanity check started the server "python manage.py runserver" in the new project and then tried to navigate to the http://127.0.0.1:8000/ ,  but it instead tries to redirect to the tutorial project's app http://127.0.0.1:8000/catalog/ and gets a 404.

I tried to install Django in the new virtual environment, but no help. Here are the errors and some other messages:

Page not found (404)

Request Method:GET
Request URL:http://127.0.0.1:8000/catalog/

Using the URLconf defined in CalendarAlerts.urls, Django tried these URL patterns, in this order:

  1. admin/

The current path, catalog/, didn't match any of these.


You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 21, 2018 - 09:28:59
Django version 2.0.1, using settings 'CalendarAlerts.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /catalog/
[21/Jan/2018 09:29:13] "GET /catalog/ HTTP/1.1" 404 1971
Not Found: /favicon.ico
[21/Jan/2018 09:29:13] "GET /favicon.ico HTTP/1.1" 404 1980

(CalendarAlert_env) C:\Users\dnintzel\Documents\django_projects\CalendarAlerts>python -m django --version
2.0.1

(CalendarAlert_env) C:\Users\dnintzel\Documents\django_projects\CalendarAlerts>python --version
Python 3.6.4


Can someone help me understand why the new project is referencing the old (and how to resolve)?
Is it related to the virtual environment? 

I am also interested in BKMs for use of virtual environments in this case? Specifically, should Django need to be installed on each virtual environment (if you don't have it installed globally?). I am actually a little surprised that Django commands executed in the new project before I installed it in that VE.

Thanks in advance,
Doug

Daniel Hepper

unread,
Jan 21, 2018, 5:24:39 PM1/21/18
to django...@googlegroups.com
It's not the new project referencing the old project, it is actually your browser caching the redirect from http://127.0.0.1:8000/ to http://127.0.0.1:8000/catalog/.
Because it is a permanent redirect, your browser won't access http://127.0.0.1:8000/, it will go http://127.0.0.1:8000/catalog/.

You can usually get rid of this redirect by clearing your browser cache. How exactly that is done depends on the browser you are using.

This also teaches an important lesson about permanent redirects. Only use them when you are absolutely sure that you (and more importantly your users) will never again want to access the old URL.

Hope that helps,
Daniel



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/772985a8-537a-4cdb-8030-177262e44efd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Hepper

unread,
Jan 21, 2018, 5:29:33 PM1/21/18
to django...@googlegroups.com
I realized that the Mozilla tutorial is a wiki, so I took the liberty to remove the "permant=True" from the redirect.

Doug Nintzel

unread,
Jan 21, 2018, 6:26:39 PM1/21/18
to Django users
That got it Daniel...thanks for the quick help. Was it " permanent=True" in particular that was the problem?
Thanks again,
Doug
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Daniel Hepper

unread,
Jan 21, 2018, 8:02:33 PM1/21/18
to Django users
Yes, kind of. There are two kinds of redirects, temporary and permanent redirects. By default Django's redirect() method returns a temporary redirect. If you pass permanent=True, it returns a permanent redirect.

So here is what happened in your case:

1. You run the MDN tutorial project and point your browser to http://127.0.0.1:8000/
2. The browser requests the path / from the server 127.0.0.1:8000 (the runserver running the MDN tutorial project) and receives a permanent redirect to /catalog/
3. Then you stop the MDN project and run your own project.
4. You then point your browser to http://127.0.0.1:8000
5. Your browser thinks "wait a minute, last time I accessed the path / on the server 127.0.0.1:8000, it returned a permanent redirect to /catalog/. I'll save my user some time and just go directly to /catalog/".

Now, if a URL returns a temporary redirect, the browser knows that this redirect is, well, temporary, so it might point to a different location the next time or there might be no redirect at all. Therefore, it must load the original URL.

In the example of the tutorial, a permanent redirect should not be used, not only because it can lead to the problem you encountered.

Imagine you use this software for your local library at http://smalltownlibrary.com/. After a while, you want to add another feature, e.g. a book shop under /shop/ where visitor can buy used books. You then want to add a homepage at / where users can select whether they want to access catalogue or the shop. It works fine for new users, but everyone who accessed the site http://smalltownlibrary.com/ before is not able to access the new homepage because their browser has cached the permanent redirect to the catalog.

Permanent redirects definitely have their place, e.g. if you moved your website to a new URL and want to tell the search engines that they should only look at the new URL. But you have to be aware that they are indeed permanent.

Hope that clarifies it a bit.

Daniel

Doug Nintzel

unread,
Jan 22, 2018, 3:46:31 AM1/22/18
to Django users
Ok, makes sense. Thank you very much for the details Daniel.
Doug

ankitkl...@gmail.com

unread,
May 18, 2018, 3:22:40 PM5/18/18
to Django users
Hi Doug,

I am new to Django and i also started with MDN Locallibrary project. Everything went fine until Django admin site but I stuck at "Creating our home page" I have written the code in the suggested way only but get below error when try to run the project. I tried taking the urls.py code from github also but it gives same issue. 

Could you please help me here.

Regards,
Ankit 


Page not found (404)

Request Method:GET
Request URL:http://127.0.0.1:8000/catalog/

Using the URLconf defined in locallibrary.urls, Django tried these URL patterns, in this order:

  1. admin/
  2. ^static\/(?P<path>.*)$

The current path, catalog/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Nitin Kumar

unread,
May 18, 2018, 4:09:33 PM5/18/18
to django...@googlegroups.com
Hi Ankit, 

You must add the urls of catalog to the project urls, locallibrary.urls.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

ankitkl...@gmail.com

unread,
May 18, 2018, 4:15:00 PM5/18/18
to Django users
Hi Nitin,

Thanks for quick response.

Please find the below code from locallibrary/urls.py

Could you please let me know, where shall i add the url.


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]


from django.urls import path
from django.contrib import admin

# Use include() to add URLS from the catalog application and authentication system
from django.urls import include


urlpatterns = [
    path('admin/', admin.site.urls),
]


urlpatterns += [
    path('catalog/', include('catalog.urls')),
]


# Use static() to add url mapping to serve static files during development (only)
from django.conf import settings
from django.conf.urls.static import static


urlpatterns+= static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


#Add URL maps to redirect the base URL to our application
from django.views.generic import RedirectView
urlpatterns += [
    path('', RedirectView.as_view(url='/catalog/', permanent=True)),
]

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Nitin Kumar

unread,
May 18, 2018, 4:35:22 PM5/18/18
to django...@googlegroups.com
you have to import include.

from django.urls import path, include

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

ankitkl...@gmail.com

unread,
May 18, 2018, 4:40:25 PM5/18/18
to Django users
Hi Nitin,

Thanks for response. 

I think, include has been imported already. Please check below. Kindly see if this not correct. (Picking these lines from earlier code). 


# Use include() to add URLS from the catalog application and authentication system
from django.urls import include

Regards,
Ankit 

James Farris

unread,
May 18, 2018, 4:41:29 PM5/18/18
to django...@googlegroups.com
This is where an IDE like PyCharm comes in handy. It will tell you right away that it doesn’t recognize something and will suggest importing that package. It does a pretty good job with its suggestions. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

ankitkl...@gmail.com

unread,
May 18, 2018, 5:18:48 PM5/18/18
to Django users
Hi James,

Thanks for suggestion. I would buy that.

Meanwhile, is there anyway, i could resolve this.

Regards,
Ankit

Nitin Kumar

unread,
May 18, 2018, 5:52:44 PM5/18/18
to django...@googlegroups.com
There is community version of pycharm.

Please write the logs that will be helpful.

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages