How to specify Django backend? - python

I do not understand what is wrong with my Django backend specification
These are my urls
from django.contrib import admin
from django.urls import path,include
from django.conf.urls import url
from store import views
urlpatterns = [
url(r'^', include('store.urls')),
url(r'^accounts', include('registration.backends.default.urls')),
path('admin/', admin.site.urls),
]
This is the tree structure
bookstore
├── bookstore
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-36.pyc
│   │   ├── settings.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── wsgi.cpython-36.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
├── requirements.txt
└── store
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│   ├── 0001_initial.py
│   ├── 0002_auto_20180604_0751.py
│   ├── __init__.py
│   └── __pycache__
│   ├── 0001_initial.cpython-36.pyc
│   ├── 0002_auto_20180604_0751.cpython-36.pyc
│   └── __init__.cpython-36.pyc
├── models.py
├── __pycache__
│   ├── admin.cpython-36.pyc
│   ├── __init__.cpython-36.pyc
│   ├── models.cpython-36.pyc
│   ├── urls.cpython-36.pyc
│   └── views.cpython-36.pyc
├── templates
│   ├── registration
│   │   ├── activate.html
│   │   ├── activation_complete.html
│   │   ├── activation_email_subject.txt
│   │   ├── activation_mail.txt
│   │   ├── registration_complete.html
│   │   └── registration_form.html
│   ├── store.html
│   └── template.html
├── tests.py
├── urls.py
└── views.py
Now when I try
python manage.py runserver
I got this
Also in terminal shows me
Not Found: /accounts
[05/Jun/2018 09:08:17] "GET /accounts HTTP/1.1" 404 5632
If I go for
url(r'^accounts/', include('registration.backends.default.urls')),
then I have
1. ^accounts/
....
The current path, accounts, didn't match any of these.
How to fix this?
How does Django backend work?
I am using 2.0.5 version.

Use path instead of url:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('store.urls')),
path('accounts/', include('registration.backends.default.urls')),
path('admin/', admin.site.urls),
]
By the way, if Django version <= 1.11, use url. (path was added after 2.0)

Try using
url(r'^accounts/', include('registration.backends.default.urls')),
url(r'^/', include('store.urls')),
Rather than
url(r'^accounts', include('registration.backends.default.urls')),
url(r'^', include('store.urls')),

Related

django python project is not using the specified template files

I know this is something dumb, but I'm a new programmer and I've been smacking my head against it for 2 hours and you will likely see it in 2 seconds so...
View AllEncountersListView which has declared template_name = 'encounter_list_all.html' is using instead 'encounter_list.html'. I know the view is being called since it prints to terminal as expected. Thanks for your time.
views.py:
class AllEncountersListView(generic.ListView):
model = Encounter
paginate_by = 20
template_name = 'encounters_list_all.html'
def get_queryset(self):
print('in allEncounterListView') #to prove the correct view is being called
return Encounter.objects.filter(user=self.request.user).order_by('-encounter_date')
urls.py:
urlpatterns = [
path('',views.index, name='index'),
path('openencounter/', views.open_encounter, name='openencounter'),
path('myencounters/', views.EncountersByUserListView.as_view(), name='my-encounters'),
path('allencounters/', views.AllEncountersListView.as_view(), name='all-encounters'),
path('encounter/<int:pk>', views.EncounterDetailView.as_view(), name = 'encounter-detail'),
path('todaysencounters/', views.TodaysEncountersListView.as_view(), name='todays-encounters'),
path('logout/', views.logout_view, name='logout'),
path('export/', views.export_data_view, name = 'export'),
]
file tree:
── Aents4
│   ├── __init__.py
│   ├── __pycache__
│   ├── asgi.py
│   ├── settings.py
│   ├── templates
│   │   ├── registration
│   │   │   └── login.html
│   │   └── temp
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── encounters
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0002_auto_20210926_1548.py
│   │   ├── 0003_alter_encounter_encounter_date.py
│   │   ├── 0004_auto_20210927_1704.py
│   │   ├── 0005_animal_max_daily.py
│   │   ├── 0006_auto_20210928_1157.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   ├── models.py
│   ├── registration
│   │   └── login.html
│   ├── static
│   │   └── styles.css
│   ├── templates
│   │   ├── base_generic.html
│   │   ├── encounters
│   │   │   ├── encounter_detail.html
│   │   │   ├── encounter_form.html
│   │   │   ├── encounter_list.html
│   │   │   ├── encounters_list_all.html
│   │   │   ├── encounter_update_form.html
│   │   │   └── encounters_list_by_user.html
│   │   ├── index.html
│   │   ├── openencounter.html
│   │   └── registration
│   │   └── login.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── manage.py
class AllEncountersListView(generic.ListView):
model = Encounter
paginate_by = 20
template_name = "encounters/encounters_list_all.html"
def get_queryset(self):
return Encounter.objects.filter(user=self.request.user).order_by('-encounter_date')
Try this please if you didn't already. Usually, when we are configuring our settings.py, we make it look in the templates directory for files, but you have subdirectory encounters which contain the template files. Try to target that subdirectory with this line:
template_name = "encounters/encounters_list_all.html"
I hope this helps; please inform us about result so we can try other solutions if needed.
Maybe it didn't throw any errors because the Django class views has that functionality such that when template name is not specified it looks at the class name and tries to figure out which template to render; maybe that is why that other template is being used. Check the Django documentation for more information.

Django Routing w/ subApplication

What I have:
a Django Project that is acting as a REST API through Django Rest Framework.
a number of Django Apps that are controlling the logic of my Postgres DB.
What I am trying to do:
Create a new Django App that represents a Service / Integration with Robinhood
Within ^^ I want to structure my logic in subApplications in order to separate all logic for a user vs a transactions vs a transfers etc ...
This entire Django App & all subApplications are APIs ONLY ... they will never need models / migrations but they will eventually communicate with the other Django Apps
CODE STRUCTURE:
Main Django Project
├── APP_holdings
├── APP_logs
├── APP_unique
├── APP_users
├── ⭐️⭐️ DJANGO
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-38.pyc
│   │   └── wsgi.cpython-38.pyc
│   ├── asgi.py
│   ├── fixtures
│   │   ├── platforms.json
│   │   └── symbols.json
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── EXAMPLE.env
├── Pipfile
├── Pipfile.lock
├── Procfile
├── README.md
├── ⭐️⭐️ SERVICE_robinhood
│   ├── README.md
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   ├── ⭐️⭐️ user
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── api
│   │   │   ├── __init__.py
│   │   │   └── login.py
│   │   ├── apps.py
│   │   ├── urls.py
│   │   └── utils.py
│   │   ├── __init__.py
│   │   └── printSomething.py
│   └── views.py
├── manage.py
├── requirements.txt
├── static
│   └── __init__.py
└── staticfiles
SETTINGS.py
INSTALLED_APPS = [
# Pre Installed
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Apps
'APP_users.apps.AppUserConfig',
'APP_holdings.apps.AppHoldingsConfig',
'APP_logs.apps.AppLogsConfig',
'APP_unique.apps.AppUniqueConfig',
# !!! --- I AM ASSUMING I AM IMPORTING THE APP INCORRECTLY (and have tried many different ways mentioned on various SO threads -> See `.apps` code --- !!!
'SERVICE_robinhood.apps.ServiceRobinhoodConfig',
'SERVICE_robinhood.user.apps.UserConfig',
# Helpers
'corsheaders',
# Django Rest Framework
'rest_framework',
'rest_framework.authtoken',
]
SERVICE_robinhood/apps.py
class ServiceRobinhoodConfig(AppConfig):
name = 'SERVICE_robinhood'
SERVICE_robinhood/user/apps.py
class UserConfig(AppConfig):
# name = 'user'
name = 'SERVICE_robinhood.user'
DJANGO.urls.py
urlpatterns = [
path('admin/', admin.site.urls),
# ...auth...
path('user/', include('APP_users.urls')),
# ...otherApps...
path('RH/', include('SERVICE_robinhood.urls'))
]
SERVICE_robinhood/urls.py
# ?? do I need to import user here and pass it differently into `include` ??
# I thought that since `users` should be a module in and of itself in the overall Django Project you could just reference the app by name
urlpatterns = [
❌ path('user/', include('user.urls')),
✅ UPDATE - FROM ANSWER: ✅ path('user/', include('SERVICE_robinhood.user.urls')),
]
SERVICE_robinhood/user/urls.py
from . import utils
urlpatterns = [
path('printSomething/', utils.printSomething, name='printSomething')
]
Error I am getting:
SERVICE_robinhood/urls.py", line 23
...
ModuleNotFoundError: No module named 'user'
I am assuming that this is an issue with how I am importing the sub application into the overall app but have added all the relevant code I can think of to uncover any other problems.
Per Amine's answer (ProfileLink) do the following in your Django App to import a subApplications routes:
SERVICE_robinhood.urls.py
urlpatterns = [
path('user/', include('SERVICE_robinhood.user.urls')),
]

Not able to load static files in django template

First off, I know there are many more questions like this in StackOverflow. But none of them seem to work. So here goes...
I am learning django through this tutorial. The tutorial is kind of old but most of the code is the same. I tried adding some CSS files to the project just like the tutorial but for some reason, it is not working for me. here is my file structure...
src
├── assets
│   └── css
│   ├── base_styles.css
│   └── index_styles.css
├── core
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── settings.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   ├── views.cpython-37.pyc
│   │   └── wsgi.cpython-37.pyc
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
├── test_app
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   ├── admin.cpython-37.pyc
│   │   ├── models.cpython-37.pyc
│   │   ├── urls.cpython-37.pyc
│   │   └── views.cpython-37.pyc
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   └── __init__.cpython-37.pyc
│   ├── models.py
│   ├── templates
│   │   └── test_app
│   │   └── test_app_home.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── templates
├── aboutpage.html
├── base.html
└── homepage.html
Please note that src is the root folder. core is the name of the project and test_app is the name of the application
The global templates reside under the src/templates/. The src/assets/css/ contains the css files for the global templates.
This is my settings.py code
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'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',
],
},
},
]
...
...
STATIC_URL = '/static/'
STAITCFILES_DIRS = (
os.path.join(BASE_DIR, "assets"),
)
this is my core/urls.py file
from django.contrib import admin
from django.urls import path, include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('test_app/', include('test_app.urls')),
path('about', views.aboutpage),
path('', views.homepage),
]
urlpatterns += staticfiles_urlpatterns()
this is my base.html file.
{% load static %}
...
...
<link rel="stylesheet" href="{% static 'css/base_styles.css' %}">
...
...
I did everything the tutorial does. I am not sure why this does not work. Could it be because of the fact that the tutorial is kind of old and I am using django 3.0.4? The only difference I could make between the two versions is using {% load static %} instead of {% load staticfiles %}.
What am I doing wrong?
in development mode add a static_root also:
STATIC_ROOT = os.path.join(BASE_DIR, 'static_cdn')
then collect your files
python manage.py collectstatic
also your browser can effect so try to run incognito or stealth mode.
In the official docs,they serve staticfiles as below
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

ImportError: No module named 'users'

When I run python /manage.py runserver , it generates the following error.
ImportError: No module named 'users'
I was thinking about this error, maybe I had a mistake about app setting.
$tree
.
├── LICENSE
├── README.md
├── functional_test.py
├── requirement
│   ├── development.txt
│   └── production.txt
├── users
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   ├── tests.cpython-35.pyc
│   │   └── views.cpython-35.pyc
│   ├── tests.py
│   └── views.py
└── wef
├── db.sqlite3
├── manage.py
└── wef
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-35.pyc
│   ├── settings.cpython-35.pyc
│   └── urls.cpython-35.pyc
├── settings.py
├── urls.py
└── wsgi.py
I think it is not a problem.
2nd, maybe I don't insert 'users' in settings.py
In settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'django_extensions',
'users',
]
I have to double check about these situations.
Here's my code:
urls.py
from django.conf.urls import url
from django.contrib import admin
from users.views import JoinUsView
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', user, name='home'),
]
users/views.py
from django.http import HttpResponse
def user(request):
return HttpResponse("hello world")
You get the import error because the users directory is not on the Python path. The easiest solution is to move the users directory into the project wef directory (the one that contains manage.py).
└── wef
├── db.sqlite3
├── manage.py
├── users
│ ├── __init__.py
│ ├── __pycache__
...
└── wef
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-35.pyc
│ ├── settings.cpython-35.pyc
│ └── urls.cpython-35.pyc
├── settings.py
├── urls.py
└── wsgi.py
This will work because ./manage.py adds the project directory to the Python path. If the users directory is outside of the project directory, then you will have to modify the python path yourself.

'polls' is not a registered namespace

Im trying to get my head around this bug which I have going through the tutorial for django 1.7 (python 3.4.0)
here is mysite/urls.py -
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls', namespace="polls")),
url(r'^admin/', include(admin.site.urls)),
)
and here is my polls/templates/polls/index.html -
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li>{{ question.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
polls/urls.py -
from django.conf.urls import patterns, include, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^(?P<question_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<question_id>\d+)/results/$', views.results, name='results'),
url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
)
My directory tree is -
.
└── mysite
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-34.pyc
│   │   ├── settings.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── wsgi.cpython-34.pyc
│   ├── settings.py
│   ├── templates
│   │   └── admin
│   │   └── base_site.html
│   ├── urls.py
│   └── wsgi.py
├── polls
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │   ├── 0001_initial.cpython-34.pyc
│   │   └── __init__.cpython-34.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-34.pyc
│   │   ├── __init__.cpython-34.pyc
│   │   ├── models.cpython-34.pyc
│   │   ├── urls.cpython-34.pyc
│   │   └── views.cpython-34.pyc
│   ├── templates
│   │   └── polls
│   │   ├── detail.html
│   │   └── index.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── urls.py
Iv'e already tried restarting the server and probably anything about this issue in stack overflow..
Removing "polls:" from "{% url 'polls:detail' question.id %}" seems like it fixes that but i don't know if it still gives me the desired functionality
Help?
Solved it, I accidentally had two urls.py files - mysite/urls.py and mysite/mysite/urls.py, I edited the top one, which is nonsesnse.

Categories