Django - Not being able to merge front end to backend - python

Im learning programming and im completely new in it..i was working on a project and i have used django for back-end. Now the problem im currenctly facing is that i got no idea how should i link frontend and backend ?..
first we created backend (where there is login/signup/and dashboard) using django and boostrap,js .. and the backend work perfectly so below the folder structure of the backend
we are working on.
so this is the structure of the backend .. to be more clear check 2nd image.
Here you can see that, budgetwebsite folder which is just below the folder authentication.
budgetwebsite is our main thing or a part of our system..
then we did django startapp for authentication(for username validation and email)
then we did django startapp for userincome(here we worked on userincome like add/delete income)
then we did django startapp for expenses(here we worked on expenses like add/delete/expense)
and that userpreference is our admin panel.
thats for the backend section
Now lets move on the front end section aswell.
then we created a different folder name front end and we started working on it.
So now lets move to the problem... i just want to merge this front end and back end ..
below is my code of
setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'expenses',
'userpreferences',
'userincome',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'main')],
'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',
],
},
},
]
WSGI_APPLICATION = 'budgetwebsite.wsgi.application'
STATIC_URL = '/static/'
STATICFILES_DIRS=[os.path.join(BASE_DIR,'budgetwebsite/static')]
STATIC_ROOT = os.path.join(BASE_DIR,'static')
MESSAGE_TAGS ={
messages.ERROR : "danger"
}
urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('', include('expenses.urls')),
path('authentication/', include('authentication.urls')),
path('preferences/', include('userpreferences.urls')),
path('income/', include('userincome.urls')),
path('admin/', admin.site.urls),
]
now my question is do i again have to startapp which i did for like expenses , income and authentication or is there any way i can easly merge my front end with backend. If you didn't understood the question im sorry ..
and if the question is that appropriate then im sorry for that aswell.
Thanks.

Let's assume you want the user to get the expense app to open when he first visits your wesite
So the expense app will have it's own views.py and you have to create a urls.py for it and also all the other apps too.
now in the urls.py file of the expense you have to write the url patternss for that app
urls.py
from django.urls import path
from . import views
urlpatterns=[
path('expense', views.expense, name= 'expense'),
]
similarly all the urls you want to display under the expense app has to written here first connected to a view function
views.py
from django.shortcuts import render
def expense(request):
return render(request, 'expense.html') #or any other html file related to expese
all the pages related to the expense app will have different view function to render them and also url patterns connected to then so that the user can request
This is the most basic codes that you have to write to render any page related to the apps you want to display

Related

How to send html-styled password reset email with django

I created an html-styled version of a password reset email to be set with django 4.0 located at 'registration/html_password_reset_email.html'. From other stackoverflows, I learned I needed to add the html_email_template_name parameter for the html version of the email to be sent. However, even with the below code, it is just the text version of the file that is being sent ('registration/password_reset_email.html'). It is definitely fining the registration/password_reset_email.html file, because edits I make to it are successfully emailed, but I can't get it to send the html version. Hints on what I'm doing wrong?
from django.contrib import admin
from django.urls import include, path
from django.contrib.auth import views as auth_views
from django.views.generic.base import RedirectView
from django.urls import reverse
from . import views
urlpatterns = [
path('', views.homeview, name="homeview"),
path('dashboard/', include('dashboard.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path("register/", views.register_request, name="register"),
path('reset_password/', auth_views.PasswordResetView.as_view(
template_name='registration/password_reset_form.html',
html_email_template_name='registration/html_password_reset_email.html',
email_template_name='registration/password_reset_email.html',
), name="reset_password"), # Submit email form
path('reset_password_sent/', auth_views.PasswordResetDoneView.as_view(), name="password_reset_done"), # Email sent success message
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name="password_reset_confirm"), # Link to password reset form in email
path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(), name="password_reset_complete"), # Password successfully changed message
]
This is part of my settings.py file
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent
# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
# Application definition
INSTALLED_APPS = [
'dashboard.apps.DashboardConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'mysite',
'users',
'corsheaders',
]
AUTH_USER_MODEL = 'users.User'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},
]
BASE_DIR resolves to: /Users/MYNAME/Documents/repos/begolden/mysite/templates
and my template files are at:
/Users/MYNAME/Documents/repos/begolden/mysite/templates/registration/html_password_reset_email.html
I tried setting EMAIL_BACKEND to django.core.mail.backends.console.EmailBackend and the text version in registration/password_reset_email.html (not the html version) prints out to console. I also tried changing the html file to just say "Hello World", in case the original html was malformed. The text registration/password_reset_email.html still prints to console (not the html file location).
I think it isn't actually using the file locations I'm providing, because when I change the email_template_name like below, it doesn't read the new text 'registration/test.html'. It still reads the text at the default location 'registration/password_reset_email.html '. I find this confusing though, because it IS finding my custom text at 'registration/password_reset_email.html', which seems to imply that my folder structure is correct but PasswordResetView just isn't using the argument names I am giving it??
path('password_reset/', auth_views.PasswordResetView.as_view(
html_email_template_name='registration/html_password_reset_email.html',
email_template_name='registration/test.html'
), name="password_result"),
You named your URL reset_password, while the name used by Django is password_reset. You have to use the same name, as it will be called by Django with reverse('reset_password'):
urlpatterns = [
...,
path('accounts/', include('django.contrib.auth.urls')),
...,
path('reset_password/', auth_views.PasswordResetView.as_view(
template_name='registration/password_reset_form.html',
html_email_template_name='registration/html_password_reset_email.html',
email_template_name='registration/password_reset_email.html',
), name="password_reset"), # <-
...,
]
Also, beware to keep this URL pattern after path('accounts/', include('django.contrib.auth.urls')), as the URL name will clash and the last one has the precedence.
When naming URL patterns, choose names that are unlikely to clash with other applications' choice of names. If you call your URL pattern comment and another application does the same thing, the URL that reverse() finds depends on whichever pattern is last in your project's urlpatterns list.

trying to override allauth templates on a Django 3.2 app recognized in python 3.9

I'm working with ALLAUTH on Django 3.2. Most solutions I found are for Django 1, so I'm hoping to find something more up-to-date here.
I have the module/app installed, but I'm having problems overriding the templates with my own.
In settings.py:
INSTALLED_APPS = [
...
'Landing.apps.LandingConfig',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google'
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'templates'
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Already defined Django-related contexts here
# `allauth` needs this from django
'django.template.context_processors.request',
],
},
},
]
AUTHENTICATION_BACKENDS = [
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
]
Research Urls #The Project
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('Landing.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('allauth.urls')),
]
Landing/urls.py #app-level urls
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('login/', views.LoginView.as_view(), name='account_login'),
path('signup/', views.SignupView.as_view(), name='account_signup')
]
Home.html
<...>
<body>
<H1>This is the homepage</H1>
<p>login</p>
<p>Create Account</p>
</body>
<...>
Note: account_login and account_signup are both in Landing urls and Landing Views
Landing Views
from django.shortcuts import render
from allauth.account.views import LoginView, SignupView
# Create your views here.
def home(request):
return render(request, 'Landing/home.html')
class LandingLogin(LoginView):
print('found Login View....')
template_name = 'authentication/login.html'
class LandingSignup(SignupView):
print('found Login View....')
template_name = 'authentication/account_signup.html'
My Tree
I can navigate to localhost:8000, and when the html comes up, two things occur:
links on home.html still point to allauth links
Landing/Home points to the custom template, but it still routes to the allauth page.
How can set the view, link, and route to the correct page?
Thanks!
In Landing/urls.py, login and signup still points to allauth views (allauth.account.views.LoginView, allauth.account.views.SignupView) and not the overridden views.
Can you try changing them from:
path('login/', views.LoginView.as_view(), name='account_login'),
path('signup/', views.SignupView.as_view(), name='account_signup')
to:
path('login/', views.LandingLogin.as_view(), name='account_login'),
path('signup/', views.LandingSignup.as_view(), name='account_signup')
If you want to override the HTML templates that are provided by allauth, you need to save your template files in templates/account/ directory instead of templates/authentication/. Then you won't need to use the template_name variable. Also make sure that the name of the HTML file corresponds to the page you want to override. For example if you want to override the login page, you need to save your template in the templates/account/ directory with the name login.html. You can take a look at the views in the allauth library to see what name you need to keep. Take a look at the documentation for more info.
And also as the answer given by #bdbd, you would need to change the view names in the urls.py file.

Django is loading template from the wrong app

I have a view under my elearning app named home(), which should load index.html from within the app's directory. Instead it loads an instance of index.html from a different app (symposium/templates/index.html). It should be loading it from (elearning/templates/index.html).
Can someone please explain why this is happening and how to fix it?
# Settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Machina dependencies:
'mptt',
'haystack',
'widget_tweaks',
# Machina apps:
'machina',
'machina.apps.forum',
'machina.apps.forum_conversation',
'machina.apps.forum_conversation.forum_attachments',
'machina.apps.forum_conversation.forum_polls',
'machina.apps.forum_feeds',
'machina.apps.forum_moderation',
'machina.apps.forum_search',
'machina.apps.forum_tracking',
'machina.apps.forum_member',
'machina.apps.forum_permission',
# SCORM apps:
'accounts',
'symposium',
'elearning']
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates'), MACHINA_MAIN_TEMPLATE_DIR,],
'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',
'machina.core.context_processors.metadata',
],
},
},
]
# root urls.py
from django.contrib import admin
from django.urls import path, include
from machina import urls as machina_urls
from accounts.views import CreateUser
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('forum/',include(machina_urls)),
path('admin/', admin.site.urls),
path('createuser',CreateUser.as_view()),
path('symposium/', include('symposium.urls')),
path('elearning/', include('elearning.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# elearning app urls.py
from django.urls import include, path
from . import views
app_name = 'elearning'
urlpatterns = [
path('', views.home, name='home'),
]
# app: elearning/views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request,'index.html',{})
# index.html location
elearning templates : ./elearning/templates/index.html
Django searches for a templates folder within each app for the relevant template to render. Looks like the first it found was index.html from within the symposium app.
The convention of directory structure for templates is to put your templates within another folder with the name of the app in order to avoid this exact situation with templates of the same name.
Move your index.html file from
/elearning/templates/index.html
to
/elearning/templates/elearning/index.html
and then change your home view function to
def home(request):
return render(request,'elearning/index.html',{})
and similarly do the same for the location of the index.html file in the symposium app.

Template file not found Django

I am trying to make a home page of a new website via Django.
My app name is 'blog', home page is home.html
I still receive the error template does not exist when I go to http://127.0.0.1:8000/blog/home/
I made sure I added 'blog' to my templates in settings.py and that I added the folder templates in the main directory as well as through blog/templates/blog/home.html
myproject/blog/views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 'blog/home.html')
myproject/blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('home/', views.home, name='home'),
]
myproject/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, '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',
],
myproject/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
Do you see anything in my code which causes a problem? I receive the message in blog/views.py that "Template file 'blog' not found" on the line
return render(request, 'blog/home.html')
You are doing it wrong. You need to read the Django documentation carefully and try to understand whatever you read and implement the same step by step.
The url you have to hit is
http://127.0.0.1:8000/blog/home/
home.html will be rendered at this url.
You don't put html page name in the url
I have been looking for answers too and I tried everything but this worked for me on my windows machine. Adding 'r' before "templates" in os.path.join(BASE_DIR, 'templates') to look like this os.path.join(BASE_DIR, r'templates') resolved the error issue. Also my templates directory was in my project root along side the parent app and sub-apps.

TemplateDoesNotExist when using Django package

I followed this Django tutorial: https://docs.djangoproject.com/en/1.10/intro/reusable-apps/.
I have a project called oldcity and an app called oldantwerp. The app is located in a parent directory called django-oldantwerp and the app directory itself has a subdirectory templates. The index.html file that my project is looking for is situated like so:
django-oldantwerp>oldantwerp>templates>oldantwerp>index.html
I tried to use this app with my project by including it in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'oldantwerp'
]
and in urls.py (in the projec), like so:
urlpatterns = [
url(r'^oldantwerp/', include('oldantwerp.urls')),
url(r'^admin/', admin.site.urls),
]
When I go to the admin page, everything works, but when I try to open the index page I get this error:
TemplateDoesNotExist at /oldantwerp/
It says it tried to locate the index.html file like so:
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.filesystem.Loader: /Users/Vincent/Apps/oldcity/templates/oldantwerp/index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /Users/Vincent/Apps/oldcity/venv/lib/python3.4/site-packages/django/contrib/admin/templates/oldantwerp/index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /Users/Vincent/Apps/oldcity/venv/lib/python3.4/site-packages/django/contrib/auth/templates/oldantwerp/index.html (Source does not exist)
And it also tried searching for another file: place_list.html, which is strange because I don't think I have such a file.
What could be wrong?
EDIT
This is the code of views.py in the oldantwerp folder:
class IndexView(generic.ListView):
template_name = 'oldantwerp/index.html'
context_object_name = 'places'
def get_queryset(self):
return Place.objects.order_by('id')[:]
EDIT
Maybe worth mentioning: it all did work when I just had a folder oldantwerp as a subdirectory in the oldicty project folder. This error only occurred after I started implementing it from an external package.
EDIT
These are my template settings in settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, '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',
],
},
},
]
One most likely problem could be you have got the template name wrong in your view. Ensure you use the template oldantwerp/index.html in the view, not just index.html.
If you look at your stacktrace you will see that you application tries to load the template from: * django.template.loaders.filesystem.Loader: /Users/Vincent/Apps/oldcity/templates/oldantwerp/index.html (Source does not exist).
If I read your first statement correctly your directory structure is:
django-oldantwerp>oldantwerp>templates>oldantwerp>index.html.
So you can either create a directory structure proper for django to identify your templates directory or you can update your templates constant from the settings file and point django to a physical location where it can find your templates.
You can try to set DIRS in settings.py like this: os.path.join(BASE_DIR, 'templates'), delete the oldantwerp folder from your templates folder and add index.html in templates.
After that, edit template_name = 'oldantwerp/index.html' to template_name = 'index.html'

Categories