Django urls.py mechanism - python

Recently I started my web project using Django with reference to some youtube tutorials. I have created a project named admin and created an app named user. From many tutorials they have created separate urls.py for both project and app. But in my case only the project's urls.py is working.
For example, I have defined the index view (my home page) in project's urls.py and login view in app's urls.py. When I try to access the login as it shows:
The current path, login, didn't match any of these
Can anyone help me to find out the solution for this?

Django only checks the URLs of the project's ROOT_URLCONF e.g. urls.py. If you want to include the URLs of a certain app, you can do the following:
In your projects urls.py:
from django.urls import include, path
urlpatterns = [
path('user/', include('user.urls')),
# others...
]
Then, if you go to url yoursite.com/user/, it will find the URLs specified in the app's urls.py. For more information see the docs.
Also make sure that your app user is in the INSTALLED_APPS of your settings.py, like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
...
'user',
...
]
So that django knows about your app.

Related

getting error : ModuleNotFoundError: No module named 'trialrisk.urls' in python

I'm new here in django python, right now I'm working with rest api, So I have created new app trialrisk, first i have added my app in settings.py file, After then when I am trying to add url in urls.py file I'm getting an error : ModuleNotFoundError: No module named 'trialrisk.urls' in python, Here I have added the whole code and my folder structure, Can anyone please look my code and help me to resolve this issue ?
Folder Structure
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'trialrisk',
]
urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('/', include('trialrisk.urls'))
]
There is no urls.py file in trialrisk folder. Create the same in trialrisk folder and import from it.
In addition to adding urls.py to trialrisk, you'll also have to add a urlpatterns object to it e.g. urlpatterns = [], or you'll get an error like this
raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) from e django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'trialrisk.urls' from '/pathtoyourproject/trialrisk/urls.py'>' does not appear to have any patterns in it. If you see the 'urlpatterns' variable with valid patterns in the file then the issue is probably caused by a circular import.

Django First Tutorial: ImportError: No module named 'polls'

I've set up Django on my Windows 10 PC, and was working through the first tutorial: https://docs.djangoproject.com/en/1.9/intro/tutorial01/
I can't seem to do the first part, because of an import error.
Here's the views.py script:
mysite/polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
urls.py in the polls app:
mysite/polls/urls.py
from django.conf.urls import url
import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
And urls.py in mysite:
mysite/mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
After troubleshooting and fixing issues with the first two files, this is the error I get when I run "python urls.py" on the mysite urls.py file:
I've seen a few stackoverflow posts here regarding this tutorial, and similar issues. Someone advocated that I add polls to the INSTALLED_APPS section of settings.py, but this did not work.
/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 also read that someone got this to work by adding the 'polls' module to their pythonpath... But I'm not sure if this is the way to go.
A very similar post was made here: Django reusable apps tutorial, ImportError: No module named 'polls'
but no solution was provided.
Anyone with some insight or input, please let me know what I can do to fix this, and let me know if you need any more information.
You seem to be running urls.py. shouldn't you be running python manage.py runserver command to start the app. Are you trying to achieve something else here ?

Django accessing admin page and adding a new url - BEGINNER

I have created a DJango project in eclipse. Later i added a new application (R-CLick Project folder ---> DJANGO ---> Create application (manage.py startapp))
I names it Super.
Then again i created another new application (using the same steps described above), and named it Human.
In my project now, i have 2 applications created (In eclipse it appears as 2 packages).
I have a file called admin.py inside the package Super.
The code is as follows:
from django.contrib import admin
from Super.models import People
from Human.models import NormalHuman
admin.site.register(People)
admin.site.register(NormalHuman)
I even registered the 2 new applications in the Settings.py file.
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'Super',
'Human',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
I also made changes to the urls.py file.
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from Human.models import NormalHuman
admin.autodiscover()
urlpatterns = patterns('',
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^normal/', NormalHuman),
)
Problems i want to solve:
1.) After restarting the server, when i try to navigate to the url 127.0.0.1:9095/normal i end up in a 404
2.) I need to add NormalHuman to the admin page, so i have access to its content.
You have already registered admin url so you do not need to add
url(r'^normal/', NormalHuman),
to see content of NormalHuman model.
just simply hit 127.0.0.1:9095/admin/human/normalhuman to see content of NormalHuman model.

Django not recognizing django admin urls

I just registered my models my models with django admin.
I navigate to the django admin at /admin. I log in sucessfully and I can see all my models. great so far.
But now if I try to click one of the links, for Ex: 'users', django gives me a 404 saying
The current URL, admin/auth/user/, didn't match any of these.
Its really weird because in my urls.py I have it mapped correctly
(r'^admin/', include(admin.site.urls)),
I have all the required middleware enabled and have these in my installed apps
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
anyone have any idea? Thanks.
Do you have this one in your urls.py?:
from django.contrib import admin
admin.autodiscover()
But in fact without this you shouldn't even see models from django.contrib.auth... weird, can you post complete urls.pt file?

Apps won't show in Django admin

I've read all the other threads but I still don't get why my apps are not showing up in Django admin. Everything else works fine.
My apps are in settings.py
I have admin.autodiscover in my root urls.py file
from django.conf.urls.defaults import *
from django.conf import settings
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', direct_to_template, {
"template": "homepage.html",
}, name="home"),
url(r'^admin/invite_user/$', 'signup_codes.views.admin_invite_user', name="admin_invite_user"),
url(r'^account/signup/$', "signup_codes.views.signup", name="acct_signup"),
(r'^account/', include('account.urls')),
(r'^profiles/', include('basic_profiles.urls')),
(r'^notices/', include('notification.urls')),
(r'^announcements/', include('announcements.urls')),
(r'^tagging_utils/', include('tagging_utils.urls')),
(r'^attachments/', include('attachments.urls')),
(r'^comments/', include('threadedcomments.urls')),
#
(r'^wayfinder/', include('wayfinder.urls')),
(r'^site/', include('jsite.urls')),
(r'^kiosk/', include('kiosk.urls')),
(r'^navigator/', include('navigator.urls')),
(r'^location/', include('location.urls')),
(r'^event/', include('event.urls')),
#(r'^news_reader/', include('news_reader.urls')),
#(r'^weather_reader/', include('weather_reader.urls')),
(r'^admin/(.*)', admin.site.root),
)
if settings.SERVE_MEDIA:
urlpatterns += patterns('',
(r'^site_media/', include('staticfiles.urls')),
)
All my apps have an admin.py file containing something like
from django.contrib import admin
from event.models import Event
class EventAdmin(admin.ModelAdmin):
list_display = (
'short_name',
'long_name',
'locations',
'categories',
'description',
'phone',
'email',
'url_source',
'url_location',
'external_ref',
'show_event'
)
admin.site.register(Event, EventAdmin)
And I have restarted the server over and over ;-)
I am building on top of Pinax, but from my reading, it shouldn't change anything. Any clue what might be wrong ?
Do you have your apps in the INSTALLED_APPS section in settings.py?
Make sure it has your apps listed there. My section reads
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.sites',
'squick.items',
'cowsite.search',
'cowsite.posts',
)
for instance. I'm pretty sure for security, they won't show up in the admin unless they are in installed apps. I think I had this same issue, where I couldn't get cowsite to show up in the admin.
The Django docs say about the admin page: "By default, it displays all the apps in INSTALLED_APPS that have been registered with the admin application, in alphabetical order"
By coincidence I had the same problem this morning. Briefly, this is what worked for me (see references for details):
In the top level directory of MyApp (ie same directory as models.py, etc.) I added a python module admin.py, containing:
from models import ThisModel, ThatModel
from django.contrib import admin
admin.site.register(ThisModel)
admin.site.register(ThatModel)
Then in mysite directory I did syncdb and runserver, and ThisModel and ThatModel were in the admin interface.
Does that work for you?
Best wishes
Ivan
** References
(I am a new member so I am allowed to post one hyperlink only!)
Django tutorial: Make the poll app modifiable in the admin
There was also a query on the Pinax google group recently titled, "How to add my app to Admin in a Pinax project?"
Are you logging in to admin as a superuser? If not, it could be a permissions problem.
Not sure which version of django you're using but the current docs suggest including the admin urls.
('^admin/', include(admin.site.urls))
For other's coming across this, I had the same issue due to grappelli.dashboard being in the installed apps but not actually installed in the virtualenv, so do a pip freeze and ensure all your requirements are actually installed.
add your app name in "settings.py" file installed app.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]
If the other solutions did not work for you, try to load your admin dashboard in a different browser. One of my apps was not displaying on the admin dashboard while I was using Google Chrome. After trying multiple answers others suggested, I decided to use Firefox instead. Voila! I was finally able to see my app on the admin dashboard.
You didn't answer Antony's question. Are you logging in as a superuser, or at least with a user with add/edit rights for the applications? If not, you won't see them.
I had the same problem, what worked for me was changing this line in urls.py:
url(r'^admin/', include(admin.site.urls)),
to
url('^admin/', include(admin.site.urls)),
(Removing the r in the first bit of code)
For some reason I am not aware of, the Polls became visible in admin after that.

Categories

Resources