Django: TypeError using manage.py [answered, syntax error] - django

I'm running Ubuntu 18.04, python3.7, and django2.1
Currently on just step 2 on Mozilla's django tutorial : https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
Checked my code multiple times and it's identical to Mozilla's code.
When I try to run python3.7 manage.py runmigrations or python3.7 manage.py runserver I get:
TypeError: bad operand type for unary +: 'list'
I've quadruple checked to make sure that my code is identical to the guides.
Please comment if it'd be helpful for me to post anymore of my code however.
locallibrary/locallibrary/urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
from django.views.generic import RedirectView
urlpatterns += [
path('', RedirectView.as_view(url='/catalog/')),
]
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Catalog/urls.py
from django.urls import path
from catalog import views
urlpatterns = [
]
Here is my traceback:
https://pastebin.com/2CzdtWzK
Please scroll all the way down to the 'raw paste data'

It was a typo in my urls.py. I used =+ instead of +=. Thanks #ger.s.brett!

Related

Base "/" not found in Django

My django project ornaments has only one app called balls. I would like the server to point to its index when I do manage.py runserver but I get this error:
Here's my urls.py:
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('balls/', include('balls.urls')),
path('admin/', admin.site.urls),
]
urlpatterns += [
path('', RedirectView.as_view(url='balls/', permanent=True)),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
I am also including the project structure. Please note that the string ball appears nowhere in my code (as per a search).
According to your screenshot, you're sending requets to http://127.0.0.1:8000/ball and this URL not found on given path path('balls/', include('balls.urls')) so try to go to this http://127.0.0.1:8000/balls

Django ModuleNotFoundError: No module named 'product'

I am absolute new to django and I have followed the example of Django documentation https://docs.djangoproject.com/en/3.0/intro/tutorial01/
but getting a problem in main project's urls.py file which is Module not found error pls help. Heres my main projects urls.py:
`from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('product.urls')),
path('admin/', admin.site.urls),
]

ModuleNotFoundError: No module named 'django.urls' not working in Django 1.9

I'm using Django 1.9
from django.contrib admin
from django.conf.urls import include, url
from slack.views import DRSWebhookTransactionView, DRSMessageView
from django.urls import path
api_patterns = ([
path('web/', DRSWebhookTransactionView.as_view()),
path('events/', DRSMessageView.as_view()),
], 'api')
urlpatterns = [
url(r'^admin/', admin.site.urls),
path('api/v1/', include(api_patterns)),
]
After running python manage.py runserver:
from django.urls import ( # noqa
ModuleNotFoundError: No module named 'django.urls'
I'm getting this error after I tried including path. If I don't include path it's not showing the error for the 'path', it's still showing the same error. Can someone tell how can I rewrite this program? and tell me what I'm doing wrong?
for django 1.9
from django.conf.urls import url
refer this

File Browser no grapelli: NameError: name 'site' is not defined

I'm following this tutorial for install django-tinymce4-lite. At the end of the tutorial there are the indications to install django-filebrowser-no-grappelli.
I use Django 2.1.1 but even though I've followed all the indications, after the installation of the file browser was shown this message:
File
"/var/www/html/dev/miosito/django/beautifulsite_v0.1.1/djangosite/djangosite/urls.py",
line 25, in
path('admin/filebrowser/', include(site.urls)), NameError: name 'site' is not defined
Here there is urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from filebrowser.sites import site #sorry I've forgot this
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('testapp.urls')), #app for my tests
path('tinymce/', include('tinymce.urls')),
path('admin/filebrowser/', include('site.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
What I've wrong?
EDIT after Yeo correction:
I've add the string that I've forgot and I've correct
path('admin/filebrowser/', include(site.urls)),
with
path('admin/filebrowser/', include('site.urls')),
but now I've this new error:
ModuleNotFoundError: No module named 'site.urls'; 'site' is not a
package
Try the following: (remove the include)
# ...
from filebrowser.sites import site
# ...
urlpatterns = [
# ...
path('admin/filebrowser/', site.urls),
# ...
]
Always refer to the package official documentation, when you encounter errors specific to the package itself. (In this case is django-filebrowser, although the main repo seems to be at django-filebrowser-no-grappelli). Blog sometime gets outdated easily. For example the guide from your link does not specify what Django version they are using. (Looking from the way the tutorial was written include, it seems to be Django<1.9 (reference)).
If you're using Django>=2, then the official document should explain the correct way to install this package.

Django URL mapping - NameError: name X is not defined

[A similar question was asked, but not marked as answered, here. I considered continuing that thread but the website told me I'm only supposed to post an answer, so it seems I have to start a new topic.] I'm trying to follow this tutorial and I'm having problems with the URL mapping. Specifically with the part described as "So best practice is to create an “url.py” per application and to include it in our main projects url.py file". The relevant, I hope, part of the folder structure, which arose by following steps of the tutorial to the letter (if possible; usage of the 'patterns' module was impossible for example) and using Django 1.10 is the following:
myproject/
myapp/
urls.py
views.py
myproject/
urls.py
The myproject/urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include(myapp.urls)),
]
The myapp/urls.py is as follows:
from django.conf.urls import include, url
urlpatterns = [
url(r'^hello/', myapp.views.hello),
]
The myapp/views.py is as follows:
from django.shortcuts import render
def hello(request):
return render(request, "hello.html", {})
However, running 'python manage.py runserver' results in the following error:
url(r'^myapp/', include(myapp.urls)),
NameError: name 'myapp' is not defined
INSTALLED_APPS in settings.py contains 'myapp'.
I'd be greatful for any tips on how to deal with the NameError! [Or any tips whatsoever that anyone might consider to be helpful!]
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which means that the import is not required.
url(r'^myapp/', include('myapp.urls')),
Since you have move the hello URL pattern into myapp/urls.py, you can remove from myapp.views import hello from myproject/urls.py.
Once you've made that change, you will get another NameError in myapp/urls.py. In this case, a common approach is to use a relative import for the app's views.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^hello/$', views.hello),
]
Make sure you have imported following modules to urls.py.
from django.conf.urls import url
from django.contrib import admin
in django 2.0
use these
from django.contrib import admin
from django.urls import path
from first_app import views
urlpatterns = [
path('',views.index, name="index"),
path('admin/', admin.site.urls),
]
your app URL has to be a string
so, here is how the code should look like.
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
also, note that from python 2 upward the regular expression is not needed.
change URL to path
from django.conf.URLs import include path
from Django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
path('^admin/', include(admin.site.urls)),
path('^myapp/', include('myapp.urls')),
]
In Django 2.1.7 here is the default urls .py file
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
so we need to add this line as well
from django.conf.urls import url
I have followed #Alasdair answers
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which
means that the import is not required.
Unfortunately, it didn't work out(I still got the name X is not defined error). Here is how I do it.
from django.contrib import admin
from django.urls import include
from django.conf.urls import url
from article import urls as article_users
from article import urls as user_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('api/article/', include(article_users)),
path('api/user/', include(user_urls)),
]
Before using the URL command be sure to first import the url from the module Urls. Then try using the runserver.
from django.conf.urls import url
from django.contrib import admin
from django.urls import path