name 'urlpatterns' is not defined when doing translation (+= i18n_patterns) - django

I want to add a language prefix in URL patterns just like the django documentation homepage. Following this example my urls.py looks like this:
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from myapp import views
from myapp.views import MyFirstView, MySecondView
myapp_patterns = [
url(r'^$', views.CategoryView, name='index'),
url(r'^(?P<name>[a-zA-Z0-9_]+)/$', MyFirstView.as_view(), name='detail'),
url(r'^(?P<name>[a-zA-Z0-9_]+)/mysecond_view/$', MySecondView, name='mysecond_view')
]
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^categories/', include(myapp_patterns)),
url(r'^', views.LandingView),
]
This works but now when I add += i18n_patterns
urlpatterns += i18n_patterns [
url(r'^admin/', admin.site.urls),
url(r'^categories/', include(myapp_patterns)),
url(r'^', views.LandingView),
]
I get the error: NameError: name 'urlpatterns' is not defined
I did add the LocalMiddleware:
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
as well as this:
LANGUAGE_CODE = 'en-us'
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
I don't understand how urlpatterns all of the sudden is not defined anymore.
What am I doing wrong here?

Primarily because of +=. There's no previous definition of urlpatterns.
You should start of with assignment = to define it.

Related

Django admin/ return 404

Starting development server at http://127.0.0.1:8000/
Not Found: /admin/
[30/May/2021 20:33:56] "GET /admin/ HTTP/1.1" 404 2097
project/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('marketability.mkbl_urls')),
path('admin/', admin.site.urls),
path(r'^ckeditor/', include('ckeditor_uploader.urls')),
]
variants path(r'admin/', admin.site.urls), and path(r'^admin/', admin.site.urls), don't works too.
project/marketability/mkbl_urls.py
from django.urls import path
from django.views.generic.base import RedirectView, TemplateView
from . import views
app_name = 'marketability'
handler404 = 'marketability.views.handler404'
urlpatterns = [
path('', views.home, name="home"),
path('<slug:cat_>/', views.page_Category_Main, name='cat'),
path('<slug:cat_>/<slug:product_>', views.page_Product, name='product'),
path('al_about.html', views.about, name="about"),
path('al_home.html', views.home, name="home"),
path('search_all.html', views.search_all, name="doorway"),
path('robots.txt', TemplateView.as_view(template_name="robots.txt", content_type="text/plain")),
path('sitemap.xml', TemplateView.as_view(template_name="sitemap.xml", content_type="text/xml")),
path('favicon.ico', RedirectView.as_view(url='/static/marketability/favicon.ico', permanent=True)),
]
project/marketability/admin.py
from django.contrib import admin
from .models import TxtHow, TxtRatings
# Register your models here.
admin.site.register(TxtHow)
admin.site.register(TxtRatings)
project/settings.py
....
NSTALLED_APPS = [
'ckeditor',
'ckeditor_uploader',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'marketability',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/marketability/patterns'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
....
superuser has created
So, i don't see any deviations from Django Documentation.
How to solve it?
thks
Your path:
path('<slug:cat_>/', views.page_Category_Main, name='cat'),
will match with admin/ and thus see admin as the value for the cat_ slug, and thus trigger that view. Likely the view for that path will try to fetch an element with admin as slug, and fail to do this, and thus raise a HTTP 404 response.
You can put the urls for the admin/ and ckeditor/ before the one with your categories:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('ckeditor/', include('ckeditor_uploader.urls')),
path('', include('marketability.mkbl_urls')),
]
Django will visit the url patterns top to bottom, and thus trigger the first path pattern that matches, in that case the one with the admin.
It might however be better to prefix your marketability urls as well, for example with:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('ckeditor/', include('ckeditor_uploader.urls')),
path('market/', include('marketability.mkbl_urls')),
]
otherwise it is thus impossible to have a category named admin.

What's the meaning of using patterns in Django URLs?

In my Django project, I find the project urls.py resolve URLs directly
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^test/', include('test.urls')),
]
but I find the app urls.py solution always use
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^new$', views.new, name='new'),
)
when I try to change app's urls.py to
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^new$', views.new, name='new'),
]
or
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
urlpatterns += patterns('',
url(r'^new$', views.new, name='new'),
)
also works, so I want to know the meaning of using patterns and which one is better.
Patterns is deprecated since 1.8 (and removed in 1.10)
from the 1.8 docs:
Deprecated since version 1.8:
urlpatterns should be a plain list of django.conf.urls.url() instances instead.

django display uploaded by user images

I have ImageUploadField
I save images at my_project/forum_attachments directory.
But when I try to display them and see by this link: http://127.0.0.1:8000/forum_attachments/1466786056166112161_Nrns2WL.jpg
I get an error
Page not found (404) Request Method: GET Request
URL: http://127.0.0.1:8000/forum_attachments/1466786056166112161_Nrns2WL.jpg
What do I do?
UPD: urls.py:
urlpatterns = [
url(r'^polls/', include('some_app.urls')),
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^about/', views.about, name='about'),
url(r'^login/$', auth_views.login, name='login'),
url(r'^user_logout/$', views.user_logout, name='user_logout'),
url(r'^index_old/', views.index_old, name='index_old'),
url(r'^forum/', views.forum, name='forum'),
url(r'^vip/', views.vip, name='vip'),
url(r'^test/', views.test, name='test'),
url(r'^forum_new/', views.forum_new, name='forum_new'),
]
First set in settings.py:
MEDIA_URL = '/forum_attachments/'
Then in your main urls.py add following piece of code:
from django.conf.urls.static import static
from django.conf import settings
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In case you've not set your MEDIA_URL & MEDIA ROOT in settings or your local settings file, you should follow the below procedure.
Settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH,'../media/')
MEDIA_URL = '/media/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
STATIC_URL = '/static/'
Now in your Urls.py, do this:
from django.conf.urls.static import static
urlpatterns = [
url(r'^polls/', include('some_app.urls')),
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^about/', views.about, name='about'),
url(r'^login/$', auth_views.login, name='login'),
url(r'^user_logout/$', views.user_logout, name='user_logout'),
url(r'^index_old/', views.index_old, name='index_old'),
url(r'^forum/', views.forum, name='forum'),
url(r'^vip/', views.vip, name='vip'),
url(r'^test/', views.test, name='test'),
url(r'^forum_new/', views.forum_new, name='forum_new'),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The 'static' was appended to the urlpatterns because your DEBUG might be set to True in your settings.py.

django-allauth facebook integration not working

I am getting the following error.
Reverse for 'facebook_channel' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
The following line is red.
{% providers_media_js %}
These are my settings from local_settings.py
SOCIALACCOUNT_PROVIDERS = \
{'facebook':
{'SCOPE': ['email', 'publish_stream'],
'AUTH_PARAMS': {'auth_type': 'reauthenticate'},
'METHOD': 'js_sdk',
'LOCALE_FUNC': lambda request: 'en_GB',
'VERIFIED_EMAIL': False}}
SOCIALACCOUNT_QUERY_EMAIL = True
settings.py
SITE_ID = 1
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
# allauth specific context processors
'allauth.account.context_processors.account',
'allauth.socialaccount.context_processors.socialaccount',
)
THIRD_PARTY_APPS = (
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.facebook',
'suit',
'debug_toolbar',
'south',
'crispy_forms',
'haystack',
'taggit',
'bootstrapform',
'sorl.thumbnail',
)
Yes, I have done the model migrations, I have the four tables that allauth creates.
Any help will be much appreciated, it's been bugging me for a while.
Updated
Main urls.py
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('useraccount.urls')),
url(r'^directory/', include('directory.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
urlpatterns += url(r'', 'directory.views.home', name='home'),
if settings.DEBUG:
import debug_toolbar
urlpatterns += patterns('',
url(r'^__debug__/', include(debug_toolbar.urls)),
)
Updated
urls.py inside useraccount app
from django.conf.urls import patterns, url, include
from django.contrib.auth.decorators import login_required, permission_required
from useraccount.views import AccountView, ProfileUpdateView, ProfileDetailView
urlpatterns = patterns('',
(r'^logout', 'django.contrib.auth.views.logout', {'next_page': 'directory_home'}),
url(r'^profile/(?P<pk>\w+)', ProfileDetailView.as_view(), name='useraccount_profile'),
url(r'^edit', login_required(ProfileUpdateView.as_view()), name='useraccount_edit'),
url(r'^dashboard', login_required(AccountView.as_view()), name='useraccount_dashboard'),
url(r'', include('allauth.account.urls')),
)
You'll need to include the correct allauth urls in your url conf
url(r'', include('allauth.urls'))

Django CMS pages throwing 404 for users who are not logged in to the admin

All the pages throw a 404 error on a site for users who are not logged in. But if I log in to the admin and go back to view the site, all the pages are fine and viewable.
I've been using Django CMS for years and haven't come across this before. The only difference with this site is the default language is french, in my settings I have:
LANGUAGES = [
('fr', 'Francais'),
]
as my LANGUAGES setting and here is my LANGUAGE_CODE
LANGUAGE_CODE = 'fr'
Here are my urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns
and my middleware...
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'cms.middleware.multilingual.MultilingualURLMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
)
What could be the cause of this?
I was in a similar situation to you: I'm using multiple languages with Django-CMS.
My issue was related to the CMS_LANGUAGES variable I'd defined: I'd simply lifted a portion of the example from the docs.
A comment on a GitHub issue helped point me in the correct direction.
I'd previously had the variable set up as:
CMS_LANGUAGES = {
...
'default': {
'fallbacks': ['en', 'de', 'fr'],
'redirect_on_fallback': False,
'public': False,
'hide_untranslated': False,
}
}
Notice the definition of the public boolean.
Also, make sure that you follow the instructions in the documentation with respect to setting up your language variables within the CMS_LANGUAGES dictionary.
Replacing the above with 'public': True got things working for me again.
Just add a plus sign :)
if settings.DEBUG:
urlpatterns += patterns('',