I am new at Django. I am trying to create basic admin panel. when I changed the urls.py file and I put url(r’^admin/’, include(admin.site.urls)), into patterns then it gave error like that:
Request Method: GET return
Request URL: http://127.0.0.1:8000/
Django Version: 1.4
Exception Type: ImportError
Exception Value:cannot import name admin
Exception Location:/home/ulascan/Desktop/projects/purple/purple/urls.py in <module>, line 1
Python Executable: /usr/bin/python
Python Version: 2.7.3
Python Path:
['/home/ulascan/Desktop/projects/purple',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2'
...
...]
Nilesh
20.4k16 gold badges90 silver badges145 bronze badges
asked Nov 19, 2012 at 12:04
2
Make sure that in your settings.py
you have django.contrib.admin
, uncommented in INSTALLED_APPS
. And also check that you have these lines in your urls.py
:
from django.contrib import admin
admin.autodiscover()
Nilesh
20.4k16 gold badges90 silver badges145 bronze badges
answered Nov 19, 2012 at 12:24
urls.py в папке проекта
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('bboard/', include('bboard.urls'))
]
Вот urls.py приложения
from django.urls import path
from . import views
urlspatterns = [
path('', views.index)
]
Ошибка
django.core.exceptions.ImproperlyConfigured: The included URLconf » does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
Syntax Error from django.contrib import admin
Hello everybody, i’m newbie in pythonanywhere, i follow the tutorial (https://www.pythonanywhere.com/wiki/DjangoTutorial) to start my project , but I can’t access http://anthraxp.pythonanywhere.com/admin/ il shows always:
It worked! Congratulations on your first Django-powered page.
Of course, you haven’t actually done any work yet. Here’s what to do next:
If you plan to use a database, edit the DATABASES setting in mysite/settings.py.
Start your first app by running python mysite/manage.py startapp [appname].
You’re seeing this message because you have DEBUG = True in your Django settings file and you haven’t configured any URLs. Get to work!
I am seeing this Error when saving urls.py Please Suggest me What to do. I am stuck, I have taken all steps carefully Please help me admins
deleted-user-117041
|
3
posts
|
Oct. 22, 2013, 9:41 a.m.
|
permalink
Where’s the syntax error? That looks like you have a working Django app.
glenn
|
9162
posts
|
PythonAnywhere staff
|
Oct. 22, 2013, 9:51 a.m.
|
permalink
When I am opening the file urls.py at line number 4 I seek a Cross mark.
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin #i am seeing error here
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
#url(r'^$', 'AskQuestion.views.home', name='home'),
#url(r'^AskQuestion/', include('AskQuestion.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)),
)
I am also not able to open admin area http://anthraxp.pythonanywhere.com/admin/
Shows this Message
It worked!
Congratulations on your first Django-powered page.
Of course, you haven’t actually done any work yet. Here’s what to do next:
If you plan to use a database, edit the DATABASES setting in AskQuestion/settings.py.
Start your first app by running python AskQuestion/manage.py startapp [appname].
You’re seeing this message because you have DEBUG = True in your Django settings file and you haven’t configured any URLs. Get to work!
I have done all the steps well from tutorial Till Defining your urls
edit by admin for formatting
deleted-user-117041
|
3
posts
|
Oct. 22, 2013, 9:57 a.m.
|
permalink
I noticed this when I edited your post to include the Python formatting: If your code block above is a direct copy of your urls.py, then you have indentation problems on the line of the error and the one that follows it. In Python, whitespace is important and you have a space at the start of those 2 lines.
glenn
|
9162
posts
|
PythonAnywhere staff
|
Oct. 22, 2013, 10:06 a.m.
|
permalink
Thank you for your support but now i am getting this error message while loading this domain: http://anthraxp.pythonanywhere.com/
ViewDoesNotExist at /
Could not import AskQuestion.views. Error was: No module named views
and long page of details load
But i have created a view in askquestions/myapp/views.py
Please tell what to do?
deleted-user-117041
|
3
posts
|
Oct. 22, 2013, 10:35 a.m.
|
permalink
I can see several possible sources of the problem:
1. The error mentions AskQuestion.views (with caps), but you give the path to the file as askquestions (lowercase and with an s)
2. Your path includes myapp in the path, but the view that could not be found does not
Check that the modules you import agree with the folder structure.
glenn
|
9162
posts
|
PythonAnywhere staff
|
Oct. 22, 2013, 2:56 p.m.
|
permalink
Loading
A NoReverseMatch error is a common Django error and one that can often be fixed with just one line of code.
NoReverseMatch at /
Reverse for 'index' not found. 'index' is not a valid view function or pattern name.
The cause of the error is likely to be in your template (a HTML file), in urls.py
or views.py
.
The NoReverseMatch error happens when Django can’t find a path in urls.py
that matches the provided name.
URLs can be referenced by name in templates, or in a view using the reverse
, reverse_lazy
, or redirect
functions.
Solving the error is a matter of making sure the correct name and the correct arguments are supplied.
What is a URL name?
In urls.py
, URL patterns take the following format:
path("filter/<str:status>", views.filter_tasks, name="filter")
This URL has a name of filter
. Whenever you need to make reference to a URL, you can use the name instead of having to write out the whole URL.
Django will take the name and look it up in urls.py
. If it finds a match, it will return the URL of the match. If it cannot find a match, it will raise a NoReverseMatch error.
Common Causes
1. Typo in urls.py or your template
This particular error has the name of the URL that couldn’t be matched:
NoReverseMatch at /
Reverse for 'index' not found. 'index' is not a valid view function or pattern name.
Go to urls.py
file and confirm that there is a URL named index:
# todo/urls.py from django.urls import path from todo import views urlpatterns = [ path("", views.index, name="index"), ]
2. You forgot to put quotations around the name
Consider this HTML snippet:
<a href="{% url filter Status.TODO 123%}">To Do</a>
This will give you the following error:
NoReverseMatch at /
Reverse for '' not found. '' is not a valid view function or pattern name.
You can fix this by putting single quotations around filter
, the URL name.
<a href="{% url 'filter' Status.TODO 123%}">To Do</a>
3. You have provided too many or too few arguments
Consider this error:
NoReverseMatch at /
Reverse for 'filter' with arguments '(Task.StatusChoice.TODO, 123)' not found. 2 pattern(s) tried: ['filter/(?P<status>[^/]+)\Z', 'filter/(?P<status>[^/]+)\Z']
This is what I provided in my template:
<a href="{% url 'filter' Status.TODO 123%}">To Do</a>
And this is the URL pattern in urls.py
:
path("filter/<str:status>", views.filter_tasks, name="filter")
Django has matched the name but my template provided 2 arguments; the URL path only expects one.
Because the number of arguments doesn’t match, Django cannot match the URL name, even though there is a URL pattern named “filter”.
The URL pattern in this example expects one argument. You can fix the error by removing the extra argument from the template.
4. Your app’s URLs haven’t been registered
Say you have checked your template and urls.py
and there definitely isn’t a typo. The link in your template has the correct syntax and the correct number of arguments have been supplied.
The next step is to check the URLs for your app have been imported into the main urls.py
.
Django projects are organised by apps. Your site directory (the folder that contains settings.py
also has a file called urls.py
that contains the index of URLs for the entire project. Each app can have its own urls.py
file, but those URLs need to be imported into the main urls.py
folder.
If you only have one app, then this won’t be the cause of your error. If you have one app and the URLs haven’t been imported into urls.py
properly then you will get this screen instead of a NoReverseMatch error:
Check your site’s main urls.py
file.
If you haven’t imported all the URLs for your app, then you will need to import include
from django.urls
and add a path to the URL patterns.
How to include your app’s URLs:
For example, my main urls.py
file looks like this:
from django.contrib import admin from django.urls import path, include urlpatterns = [ path("admin/", admin.site.urls), path("", include("sample.urls")), path("", include("todo.urls")), ]
5. Error in “reverse”, “reverse_lazy” or “redirect”
NoReverseMatch errors don’t just happen in templates. They can happen anywhere in your code that tries to get a URL from a URL name. They are often used in views.py
to redirect the user.
Three common functions that do this are:
- Reverse
- Reverse Lazy
- Redirect
The error could be in the name of the URL or the arguments supplied.
Consider this URL pattern:
path("filter/<str:status>", views.filter_tasks, name="filter")
It has a name of “filter” and accepts one argument- a string called “status”.
Because the URL pattern has parameters, reverse("filter")
will return a NoReverseMatch error.
Instead, you must provide the “status” argument:
url = reverse("filter", args=["complete"])
The following is also acceptable if you prefer to name your arguments:
url = reverse("filter", kwargs={"status": "complete"})
Conclusion
NoReverseMatch errors occur when you are trying to get the URL from its name.
This error often occurs in templates, when you are trying to add a link to another page, or in views.py
when you want to redirect a request to another view.
In both cases, it can be as simple as checking the name against urls.py
. If the name matches, then check if the correct number of arguments have been supplied. Providing too many or too few arguments will trigger the error.
If that still hasn’t fixed the issue, then check that the URLs of you app have been included in your site’s main urls.py
(the one in the same folder as settings.py
). If the urls.py
belonging to an app hasn’t been included, then Django won’t be able to find it.
Understand More
If you are new to Django, then you may find some examples helpful.
This article from my To Do list tutorial goes through setting up URLs for the first time.
This article from the same tutorial series, gives an example of how to implement URLs with multiple arguments.