Django doesn't see urls.py - django

I'm new to Django and I'm following this tutorial: https://www.django-rest-framework.org/tutorial/1-serialization/
When I run the server with python manage.py runserver, it seems to work fine showing me no errors.
However when I visit http://127.0.0.1:8000/snippets/ it gives me the following error:
Using the URLconf defined in tutorial.urls, Django tried these URL patterns, in this order:
admin/
The current path, snippets/, didn’t match any of these.
Here's my urls.py located in the tutorial folder:
from django.urls import path, include
urlpatterns = [
path('', include('snippets.urls')),
]
I don't understand what's going on, how come it only checks admin/ if I'm wiring the root urlconf to snippets.urls?
On a related note, when I modify urls.py and add gibberish the server won't give me an error, however if I were to modify models.py then it'll start complaining, that's why I'm getting the feeling it doesn't even check my urls.py and maybe instead some default one...
Any tips are very appreciated!
EDIT:
Here's my urls.py located in the snippets folder:
from django.urls import path
from snippets import views
urlpatterns = [
path('snippets/', views.snippet_list),
path('snippets/<int:pk>/', views.snippet_detail),
]
In settings.py I've only modified the INSTALLED_APPS part with the two last apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'snippets.apps.SnippetsConfig',
]

Are you sure you don't have another instance of Django running?
That's the only thing I can think of since admin isn't even in your current urls

Related

Django tutorial problem: The current path, polls/, didn’t match any of these

I'm just getting started with Django, and I'm a touch rusty with web development, so this may be an easy one. I'm stepping through the Django Polls Tutorial from the official documentation and I encounter a problem nearly right away. I'm not having success accessing http://localhost:8000/polls/ . I receive the error...
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/polls/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The current path, polls/, didn’t match any of these.
Here is my relevant code...
\mysite\polls\views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
\mysite\polls\urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
\mysite\urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
\mysite\mysite\setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
ROOT_URLCONF = 'mysite.urls'
Development server reads...
Not Found: /polls/
[06/Dec/2022 15:02:01] "GET /polls/ HTTP/1.1" 404 2095
I have tried a hodgepodge of fixes that I've seen from other similar tutorial fixes, but nothing has worked and I feel like I'm taking stabs in the dark at this point.
Thank you Hash & Carlos for taking a look.
I modified as suggested, and restarted server, but I get the exact same result...
\mysite\mysite\settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
I feel as though there is a simple path error occurring somehow. Could there have been an error in my django setup? I do see the spaceship at localhost:8000, as I should.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls',
]
you are written the code correclty but you need to add your apps in intalled apps in settings.py file
I deleted my project, and my virtual environment(ok because this was the only thing in it), and started over from scratch. This time I was successful. I'm not completely sure why. It is not clear to me where the problem was, but I am ok moving on to bigger and better challenges. Ty for your responses!
BTW: I did not have to add "polls" to the settings.py file, which is consistent with the tutorial

Adding an aliasName for url name app django

In a django project, I have an app : app_signup.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app_signup',
app_signup/urls.py
from django.urls import path, include
from . import views
urlpatterns = [
path('',views.index, name='index'),
]
Is it possible to add an alias to the app in order to see the alias (http://localhost:8000/alias/) instead of the name of the app(http://localhost:8000/app_signup/) in the url on the browser.
The name of the app in INSTALLED_APPS doesn't really matter. Your project's main urls.py file is the one that specifies the path. There when constructing the urlpatterns list, you can do path('alias/', include('app_signup.urls')) instead of path('app_signup/', include('app_signup.urls')).
As far as I understand from the example you added, you are editing the urls.py file within the app you created. Not your app, but directly in the project file (in the same directory as settings.py), urls.py probably as path('app_signup/', include('app_signup.urls')) as mentioned in the answer above. This is the urls.py you need to edit.

ModuleNotFoundError: No module named 'polls'

I am following the official Django tutorial on https://docs.djangoproject.com/en/2.2/intro/tutorial01/ but somehow I am not able to run the server as I have created the polls app and added the required urls. When I use the command "py mysite\manage.py runserver" it returns me ModuleNotFoundError: No module named 'polls' error.
Project Folder available at
https://i.stack.imgur.com/bbxfW.png
#views.py
from django.http import HttpResponse
def index(request):
return HttpResponse('<h1><this is a test page</h1>')
#urls.py in polls
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
#urls.py in mysite\mysite
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
#settings.py
INSTALLED_APPS = [
'polls',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
[1]: https://i.stack.imgur.com/bbxfW.png
Well, I resolved it myself. Polls module was not found because it was created outside the project directory. When I removed it and recreated inside the project directory, it was OK now.
also it may be a good idea to cd into yoru django project so you are only running
python manage.py runserver

dojango test install results in 404

I made a fresh install of django 1.6.2 in it's own virtualenv. No syncdb, no other configs. The only other apps installed are pip, setuptools and dojango. I followed the instructions in this wiki page https://github.com/klipstein/dojango/wiki/Gettingstarted
when I try 127.0.o.1:8000/dojango/test/ or localhost:8000/dojango/test I get
Page not found (404)
Request Method: GET
Request URL: http://127.0.o.1:8000/dojango/test/
Using the URLconf defined in dojango.urls, Django tried these URL patterns, in this order:
^dojango/ ^dojango/
The current URL, dojango/test/, didn't match any of these.
this is my INSTALLED_APPS
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dojo_tutor',
'dojango',)
and this is my urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^dojango/', include('dojango.urls')),
# url(r'^admin/', include(admin.site.urls)),
)
The wiki page says to use (r'^dojango/', include('dojango.urls')), but I tried both like that and url(r'^dojango/', include('dojango.urls')),. I still get 404
Ok, it was a silly mistake. Foolishly I had named my project dojango so naturally there was a naming conflict. Deleted the project and recreated it with another name. Now it works.

Django 1.5 and Heroku: My admin main has no links to anything

As I said above, I'm using Django 1.5 and Heroku. Everything works fine locally, but in the Heroku admin I just get the main page and that's it. There are no links to my app or anything in the "auth" category or ANYTHING. The image below is what comes up.
As you can see, no links, no buttons, nothing but text.
Has anyone experienced before or have any ideas? I would be happy to post my code, but I don't want to overload this with useless junk so ask and I'll be happy to add whatever info you might want to see.
Some basic things:
Settings.py snippets:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
#'gunicorn',
#'django_evolution',
#Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'CRM',
)
urls.py snippet:
urlpatterns = patterns('',
#Uncomment the next line to enable the admin:
url(r'^/', include('CRM.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.STATIC_ROOT,
}),
)
admin.autodiscover()
Everything is registered in my admin.py
admin.site.register(Client, ClientAdmin)
admin.site.register(Project, ProjectAdmin)
Thanks!
I figured it out! Yay! For those of you in the near or distant future that suffer from this, you need admin.autodiscover() at the TOP of your urls.py file.