I recently worked on a project that had an app named 'catalog'. While I was working on it, I changed the URLconf so that the root URL could be redirected to the app.
I wrote the below code to do this:
# within the project's URLconf file
from django.views.generic import RedirectView
urlpatterns += [
url(r'^$', RedirectView.as_view(url='/catalog/', permanent=True)),
]
I was working on the local development server, and the root URL (127.0.0.1:8000) was successfully redirected to the 'catalog' app (127.0.0.1:8000/catalog/).
However, when I created a new project, the root URL of this NEW project ALSO tried to redirect to the 'catalog' app of the previous project.
So where as I should be seeing the "it worked!" page at the root URL for the new project, I am instead redirected to the 'catalog' app's URL of the previous project, where the 404 page is displayed (obviously, because the 'catalog' app is not part of the new project).
Instead of this:
It Worked Page
Seeing this:
404 Page
It seems to me that the settings from the previous project have somehow affected the local server permanently so that the modified URLconf setting is carried on to any subsequent projects.
I could not find exactly what was causing this issue so I just ran the new project on a different port (8001) using the
python manage.py runserver 8001
command, and this seemed to fix the issue. However, I regard this only as a temporary workaround and I want to find out the root cause of the issue.
If I can't fix it, I would like to "reset" the default port (8000) so everything goes back to default settings.
Is there a way to completely "reset" either the local server or django itself so that all the settings go back to how they were released?
Related
I am following an online course on Django, and my browser fails to load the admin page. I created a job, added it to INSTALLED_APPS in settings.py, and added it in admin.py as follows:
from .models import Job
# Register your models here.
admin.site.register(Job)
In urls.py I believe I have the standard admin url
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', jobs.views.home, name='home'),
]
I'm not sure if any other any information is needed but please ask if there is anything else. I start the server using python manage.py runserver and it can access /home okay, but if I change the url to admin or admin/ it says 'Firefox can’t establish a connection to the server at 127.0.0.1:8000.'.I've tried it on different browsers and the same outcome is reached. My question is similar to the one here: Django error browser is unable to connect at 127.0.0.1:8000/products/ and I tried all of the solutions there but none worked. I followed the tutorial very specifically, so I have no idea what is causing this. I am using Atom on a Windows 10 machine, the project is connected to a postgresql database.
UPDATE/EDIT 08/23/20 10:31AM PST.
Here is a screenshot of INSTALLED_APPS:
There is no error in console when I try to open admin. If I run the server as shown below and open the link http://127.0.0.1:8000/admin, the server just stops working and my command line returns to just taking in commands. Details in screenshot below.
UPDATE/EDIT 08/23/20 12:07PM PST.
I've been doing a lesson on udemy trying to make a clone site of producthunt using django. I've tried asking there, but I don't get an answer. For some reason when I run the exact same code as the instructor, I get an error when trying to load the page localhost:8000/signup or any other pages other than the home page.
I get this error:
error
Settings file:
settings
Main urls:
main urls
app urls (named accounts):
app urls
views:
app views
finally my file structure for reference:
directory
I've been trying to figure it out with no avail. Any help would be great thank you.
signup is a route in the accounts app. In your main urls.py you include your accounts.urls under accounts/. Putting that all together, with your current structure you should be hitting accounts/signup rather than just signup.
There's no url /signup in your web.
You're url is for accounts/signup/
I am new to django..
When I was working before 2 days..it was working properly..
Now django index page shows ..
ViewDoesNotExist at /
Could not import mysite.views.home. Parent module mysite.views does not exist.
my url.py contain
url(r'^$', 'mysite.views.home', name='home'),
please help me.
Where I did mistake??
Is the views.py that you are referring to in the project root? Error message says it can't find that file so check the path. If you moved the home view into an app folder, make sure you configured your settings file to include the app and point to the app folder as app_folder.views.home
Seems like a mistake in your views.py. Could you add the home view code?
I trying to add to my project django log object to my django project
I followed these steps
1)pip install django-object-log
2)copied the object_log folder into your Django project.
3)Add "object_log" to INSTALLED_APPS
4)Run ./manage.py syncdb
5)add this url to my project urls ....url(r'yasmina', include('object_log.urls')),
But i don't know what to do next so any help plz ??
and this is one of the urls in url.py of the django object log ...url(r'^user/(?P\d+)/actions/?$', 'list_user_actions', name="user-object_log-actions"),
when i runned the server and added this url to my browser http://localhost:8000/yasmina/user/2/actions/
I got error this url doesnt match
Looks like your Url pattern matches
http://localhost:8000/user/2/actions/.
When I was using the built-in simple server, everything is OK, the admin interface is beautiful:
python manage.py runserver
However, when I try to serve my application using a wsgi server with django.core.handlers.wsgi.WSGIHandler, Django seems to forget where the admin media files is, and the admin page is not styled at all:
gunicorn_django
How did this happen?
When I look into the source code of Django, I find out the reason.
Somewhere in the django.core.management.commands.runserver module, a WSGIHandler object is
wrapped inside an AdminMediaHandler.
According to the document, AdminMediaHandler is a
WSGI middleware that intercepts calls
to the admin media directory, as
defined by the ADMIN_MEDIA_PREFIX setting, and serves those images.
Use this ONLY LOCALLY, for development! This hasn't been tested
for
security and is not super efficient.
And that's why the admin media files can only be found automatically when I was using the test server.
Now I just go ahead and set up the admin media url mapping manually :)
Django by default doesn't serve the media files since it usually is better to serve these static files on another server (for performance etc.). So, when deploying your application you have to make sure you setup another server (or virtual server) which serves the media (including the admin media). You can find the admin media in django/contrib/admin/media. You should setup your MEDIA_URL and ADMIN_MEDIA_URL so that they point to the media files. See also http://docs.djangoproject.com/en/dev/howto/static-files/#howto-static-files.
I've run into this problem too (because I do some development against gunicorn), and here's how to remove the admin-media magic and serve admin media like any other media through urls.py:
import os
import django
...
admin_media_url = settings.ADMIN_MEDIA_PREFIX.lstrip('/') + '(?P<path>.*)$'
admin_media_path = os.path.join(django.__path__[0], 'contrib', 'admin', 'media')
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^' + admin_media_url , 'django.views.static.serve', {
'document_root': admin_media_path,
}, name='admin-media'),
...
)
Also: http://djangosnippets.org/snippets/2547/
And, of course, #include <production_disclaimer.h>.