I'm setting up the django admin on a project.
I've created admin.py files in each of my apps (just as I have on previous projects). However the admin.py modules are not being loaded. My models aren't registered and I can't break into the code with pydevd.settrace().
If I move my admin code to the end of models.py everything works as expected, the models are registered with admin, and the code runs (I can step through it with pydevd.settrace()).
So this fails -
my_project_app
__init__.py
cart
__init__.py
models.py
admin.py
But when I add my code to the end of the models.py file everything runs fine -
from django.contrib import admin
class CartAdmin(admin.ModelAdmin):
pass
admin.site.register(Cart, CartAdmin)
Obviously I'm going to be configuring the admin so I don't want everything in one module. How do I get my admin.py file working? And why have they stopped working (this is the first time I've used django 1.5 - not sure if that's relevant)
Usually you call admin.autodiscover() in your urls.py and it loads everything:
# urls.py
from django.conf.urls import patterns, include
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
Since you have to do it before trying to use admin.site.urls, that's the best place to call it.
In your admin.py file you have to import your models
from cart.models import Cart
If still it didnot work then Check if you have included
'django.contrib.admin',
in your installed_apps list of settings file then in urls file include
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
run manange.py syncdb
run manage.py runserver
check 127.0.0.1:8000/admin
It should work
Related
I just have started learning django in its 1.10 version. In a project (called refugio) I have created an app called mascota.
This is my views.py file for my app:
from __future__ import unicode_literals, absolute_import
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Index")
Also, I already have written my urls.py file for it:
from django.conf.urls import url
from apps.mascota.views import index
urlpatterns = [
url(r'^$/', index),
]
And I have modified the url.py file of my project:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('apps.mascota.urls')),
]
But when I run the server of my project, it sends me the next error message:
If it helps, My dir tree is the following:
REFUGIO
apps
mascota
views.py
urls.py
refugio
settings.py
urls.py
manage.py
I know this is a dummy question, but I don't know what is wrong, I have already checked my urls sentences, but I see everything ok.
I will appreciate your help.
Ps.: My app is located inside a folder called apps.
Regards.
Every Python package need __init__.py file (read this).
REFUGIO
apps
mascota
__init__.py
views.py
urls.py
__init__.py
refugio
__init__.py
settings.py
urls.py
manage.py
Change your file structure into
REFUGIO
mascota
views.py
urls.py
refugio
settings.py
urls.py
manage.py
and then change the line in urls.py
from apps.mascota.views import index
into
form mascota.views import index
Hope this helps.
There are many similar questions posted already, but I've already tried those solutions to no avail. I'm working through a basic Django tutorial, and here is my code:
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'tango_with_django_project.views.home', name='home'),
# url(r'^tango_with_django_project/', include('tango_with_django_project.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^rango/', include('rango.urls')), # ADD THIS NEW TUPLE!
)
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Rango says hello world!")
From the settings.py file
ROOT_URLCONF = 'tango_with_django_project.urls'
Hope you all can help get me started
Let's say I have a Django project called FailBook, with two apps, posts and links. If I look into FailBook/urls.py, I will find something like
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^posts/', include('posts.urls')), ## Custom url include
url(r'^links/', include('links.urls')), ## Custom url include
)
So then, when you look into the directory structure, you will notice that there are extra two urls.py files
FailBook
|-- posts
|-- models.py
|-- urls.py
|-- views.py
|-- etc.
|-- links
|-- models.py
|-- urls.py
|-- views.py
|-- etc.
# urls.py file in the posts folder
from django.conf.urls import patterns, include, url
from .views import PostListView, PostDetailView
urlpatterns = patterns('',
url(r'^posts/', PostListView.as_view()),
url(r'^posts/(?P<post_id>\d+)', PostDetailView.as_view()),
)
# where both views are class based views, hence the as_view function call
I know this was already solved, but the solutions provided did not help me. When I had this error it was as simple as checking all of the directories that should have had urls.py files.What I discovered was that the urls.py had not been added to the SVN repository that our Django app was pulled from.
I recommend looking in the projectname->projectname->urls.py for all references to app specific urls, and verifying that the urls.py file exists for each of them.
I had this issue while doing a Pluralsight Django tutorial. Two things I noticed:
1) I was using Django 2.0, and the command url() from Django 1.11 has been replaced with re_path() in 2.0, obtained by importing as follows:
from django.urls import path, include, re_path
This replaces,
from django.conf.urls import url, include
see this: Django 2.0 release notes
and,
2) I accidentally called the file to be imported from the subfolder, url.py, not urls.py
e.g. rango/url.py not rango/urls.py.
This was probably the main issue, and all flowed smoothly after that fix.
I am trying to develop a job portal in Django. As I am new to Django, I am not able to figure out why import error is getting displayed upon page hit, after deploying it on server. It was however working fine when I was running and testing in eclipse environment.
Here is the project tree structure for your reference. I know its quite long.
As you can see, MeraJob is the main project name and accounts, companies, MeraJob, students are applications inside it.
I deployed this structure and when I hit, I get this error.
I have ensured all the urls.py files have imported views.py using from views import * or simply import views. I can't figure out what the problem is, can someone help me out in this regard? Thanks in advance.
EDIT
Here is the my MeraJob/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from . import views
import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),url(r'^login/$', 'django.contrib.auth.views.login', name='login_view'),url(r'^password/reset/$', 'django.contrib.auth.views.password_change'),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT }),
url(r'^logout/$', logout_page),
url(r'^accounts/password/reset/$', 'django.contrib.auth.views.password_change'),
url(r'^password-changed/$', 'django.contrib.auth.views.password_change_done'),
url(r'^$', main_page),
url(r'^contact/$', contact_page),
url(r'', include('companies.urls')),
url(r'', include('miscellaneous.urls')),
url(r'', include('students.urls')),
)
Silly problem!
I missed to observe that the python file (views.py) didn't have read permissions for others!
Did a chmod appropriately and it worked! Thanks for other suggestions!!!
try from . import views
it would be easier if you posted your urls.py
I am trying this with Django 1.4 on Windows 7 with the default web server.
The site starts with no errors. but when I browse to localhost:8000
I get the following error
ImportError at /
No module named urls
I see where the error comes from
It is in the main URLs.py file - (r'^admin/', include('django.contrib.admin.urls')),
If I remove that form URLs.py file the home page comes up. I don't see urls.py file in "C:\Python27\Lib\site-packages\django\contrib\admin" folder. So, the error makes sense.
But that line has to be there to get Django-Registration package working. All the blogs I read about has that line. How do I get pass this? Thanks so much for your error.
Fixed the url.py per Siva's instructions below. but no luck.
from django.conf.urls import patterns, include, url
from SOWLAPP.views import *
from CATALOG.views import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'SOWL.views.home', name='home'),
# url(r'^SOWL/', include('SOWL.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url (r'^user/(\w+)/$', user_page),
(r'^login/$', 'django.contrib.auth.views.login'),
(r'^catalog/$', home),
(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root' : 'C:/SHIYAM/Personal/SuccessOwl/SOWL0.1/SOWL/SOWL/static'}),
(r'^admin/', include('django.contrib.admin.urls')),
(r'^accounts/', include('registration.urls')),
(r'^$', main_page),
)
Regards,
SHM
Check the ROOT_URLCONF entry in your settings file. The following links might help you.
https://docs.djangoproject.com/en/dev/ref/settings/#root-urlconf
How to set correct value for Django ROOT_URLCONF setting in different branches
But Your urls.py looks messy. 2 entries for same url?
url(r'^admin/', include(admin.site.urls)),
(r'^admin/', include('django.contrib.admin.urls')),
The following entry should comes at the end.
(r'^$', main_page),
Try to comment out each line inside:
urlpatterns = patterns('',
...
)
if it does not solve the error Than (as I've expected) error in the urls.py imports...
I suspect they are somewhere here:
from SOWLAPP.views import *
from CATALOG.views import *
Try to render main page without those modules imported in the urls.py
Also please provide your project settings.py config. That is under section INSTALLED_APPS. There might be an app that has no urls.py and something is referring there. But it's last case that may be... (IMHO)
As the second thought you might have the corrupted/changed Django or/and some apps redistributable version... Check if you installed them via automatic scripts. They rarely have serious bugs.
as a django newbie (I have some exprience with other python webframework like turbogears and bottle but exploring django) I'm trying to auto create the admin management for my app model
in tha main URLS.py I have:
edit:
from django.contrib import admin
admin.autodiscover()
and after that:
urlpatterns = patterns('',
url(r'^appname/',include('appname.urls')),
url(r'^admin/',include(admin.site.urls))
notice this is in the main urls.py and not in the app urls.py
following the tutorial (which did work for me in the tutorial..) I created an 'admin.py' file in the appname folder and there:
from appname.models import Appname
from django.contrib import admin
class appnameAdmin(admin.ModelAdmin):
fieldsets = [various field sets and fields etc ]
admin.site.register(Appname,AppnameAdmin)
and in setting.py I have uncommented
'django.contrib.admin'
I don't get any error in the commandline window and the basic admin screen does appear (auth and sites)
I checked the imports in admin.py in the manage.py shell and everything seemed to work allright, I also tried commenting AppnameAdmin class out and registring just:
admin.site.register(Appname)
but that didn't work eith
I'm guessing I'm missing something obvious - I'll be glad to help with that
using django 1.4 + python 2.72
Check all of these:
There are seven steps in activating the Django admin site:
Add 'django.contrib.admin' to your INSTALLED_APPS setting.
The admin has four dependencies - django.contrib.auth, django.contrib.contenttypes, django.contrib.messages and
django.contrib.sessions. If these applications are not in your
INSTALLED_APPS list, add them.
Add django.contrib.messages.context_processors.messages to TEMPLATE_CONTEXT_PROCESSORS and MessageMiddleware to
MIDDLEWARE_CLASSES. (These are both active by default, so you only
need to do this if you’ve manually tweaked the settings.)
Determine which of your application’s models should be editable in the admin interface.
For each of those models, optionally create a ModelAdmin class that encapsulates the customized admin functionality and options for
that particular model.
Instantiate an AdminSite and tell it about each of your models and ModelAdmin classes.
Hook the AdminSite instance into your URLconf.
Do you have all the other admin dependencies in your installed apps?
Do you have admin.autodiscover() in your URLS.py?
Also, I think your code should look something more like this:
from projectname.appname.models import Appname
from django.contrib import admin
class AppnameAdmin(admin.ModelAdmin):
fieldsets = [various field sets and fields etc ]
admin.site.register(Appname,AppnameAdmin)
Have you restarted the server process?
Maybe this helps someone: in my case the problem was solved by stopping and starting the server process, because when you add a new admin.py file it does not reload automatically.
aaargghhh - I found the problem. I saved admin.py in the template/appname/ folder instead of the appname/ folder. so stupid of me. so sorry for the interruption.
Set this in your model admin:
def has_add_permission(self, request, obj=None):
return True
def has_change_permission(self, request, obj=None):
return True
def has_delete_permission(self, request, obj=None):
return True
Check all these:
Restart the server and check again
Add 'models.Model' as a parameter in your class in models.py file
class Classname(models.Model):
Add your app in the 'INSTALLED_APPS' in settings.py, In my case it's 'travello.apps.TravelloConfig'
INSTALLED_APPS = [
'travello.apps.TravelloConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
Add 'admin.autodiscover()' in main urls.py
from django.contrib import admin
from django.urls import path, include
admin.autodiscover()
urlpatterns = [
path('', include('travello.urls')),
path('admin/', admin.site.urls),
]
it worked for me!
Nothing above worked for me.
Then I went to Settings, and under Project:MyApp Project Interpreter
I switched the Project Interpreter from Python 3.8(venv) to Python 3.8(MyApp)
And then all my models where registered (could list them in http://localhost:8000/admin/)
Strangely enough, still in admin.py, after "admin.site." the method registered will not be listed as available. But it works anyway.
Go to models.py file and add 'models.Model' as a parameter in your class .
Example:
class className(models.Model)
In your case use below as class name and it will 100% work for you.
class AppnameAdmin(models.Model):
from django.contrib import admin
from .models import modelname
admin.site.register(modelname)
import models in this way
Please consider that some models can be seen only from a superuser.
Try to create one and log-in the admin with that user.
python3 manage.py createsuperuser