here's my files:
from django.shortcuts import render
from django.urls import reverse_lazy
from . import forms
from django.views.generic import CreateView
from django.views.generic import TemplateView
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy("login")
template_name = "webportal/signup.html"
class HelloPage(TemplateView):
template_name = "hello.html"
2.apps urls.py:
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = "webportal"
urlpatterns = [
path("logout/", auth_views.LogoutView.as_view(), name="logout"),
path("", views.SignUp.as_view(), name="signup"),
path(
"login/",
auth_views.LoginView.as_view(template_name="webportal/login.html"),
name="login",
),
]
3.models.py:
from django.db import models
from django.contrib import auth
class User(auth.models.User, auth.models.PermissionsMixin):
""" this is account User model"""
def __str__(self):
return "@{}".format(self.username)
4.forms.py:
from django.db import models
from django.contrib import auth
class User(auth.models.User, auth.models.PermissionsMixin):
""" this is account User model"""
def __str__(self):
return "@{}".format(self.username)
5.templates->webportal(myapp's name)->1.login.html ,
{% extends "base.html" %} {% load bootstrap3 %} {% block content %}
<div class="container">
<h1>Login In</h1>
<form method="POST">
{% csrf_token %} {% bootstrap_form form %}
<input type="submit" value="Login" class="btn btn-default" />
</form>
</div>
{% endblock content %}
2.signup.html
{% extends "base.html" %} {% load bootstrap3 %} {% block content %}
<div class="container">
<h1>Sign Up</h1>
<form method="POST" action="{% url 'webportal:login' %}">
{% csrf_token %}{% bootstrap_form form %}
<input type="submit" value="Sign Up" class="btn btn-default" />
</form>
</div>
{% endblock content %}
<!-- -->
6.project's urls.py :
from django.contrib import admin
from django.urls import path, include
from rest_framework.authtoken.views import obtain_auth_token
from django.contrib.staticfiles.urls import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
from . import views
from . import settings
urlpatterns = [
path("", views.HomePage.as_view(), name="home"),
path("admin/", admin.site.urls),
path("hello/", views.HelloPage.as_view(), name="hello"),
path("thanks/", views.Thankspage.as_view(), name="thanks"),
path("core/", include("core.urls", namespace="core")),
path("webportal/", include("django.contrib.auth.urls")),
path("webportal/", include("webportal.urls", namespace="webportal")),
]
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Actually, i have my custom login template so, In app's urls.py , I have passed argument template_name accordingly in as_view method. I dont why django engine searching for registration/login.html..
My errors:
Internal Server Error: /webportal/login/
Traceback (most recent call last):
File "C:\Users\pandi\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\pandi\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\pandi\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
response = response.render()
File "C:\Users\pandi\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\response.py", line 105, in render
self.content = self.rendered_content
File "C:\Users\pandi\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\response.py", line 81, in rendered_content
template = self.resolve_template(self.template_name)
File "C:\Users\pandi\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\response.py", line 63, in resolve_template
return select_template(template, using=self.using)
File "C:\Users\pandi\Anaconda3\envs\myDjangoEnv\lib\site-packages\django\template\loader.py", line 47, in select_template
raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: registration/login.html
[18/Mar/2020 19:06:01] "GET /webportal/login/ HTTP/1.1" 500 86610
7.settings.py file
"""
Django settings for drf_api project.
Generated by 'django-admin startproject' using Django 2.2.5.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, "images")
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "y&^-h50qul((6$puv28xqy_a_fb*m4vr)y0copvu6d^o*c+8x&"
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# "debug_toolbar",
"core",
# "ingredient",
"webportal",
"rest_framework",
"rest_framework.authtoken",
"rest_auth",
"bootstrap3",
]
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",
# "debug_toolbar.middleware.DebugToolbarMiddleware",
]
ROOT_URLCONF = "drf_api.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [MEDIA_ROOT, 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",
],
},
},
]
WSGI_APPLICATION = "drf_api.wsgi.application"
# Database
DATABASES = {
"default": {
"ENGINE": "django.db.backends.mysql",
"NAME": "test_database",
"USER": "root",
"PASSWORD": "",
"HOST": "",
"PORT": "",
"OPTIONS": {"init_command": "SET sql_mode='STRICT_TRANS_TABLES'"},
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = "/static/"
MEDIA_URL = "/images/"
STATICFILES_DIR = [os.path.join(BASE_DIR, "static")]
REST_FRAMEWORK = {
" DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.TokenAuthentication"
]
}
# this internal ips helps us by not letting others see that if we went into production
# in this way we can only get toolbar in our local machine only
# INTERTNAL_IPS = ["127.0.0.1"]
LOGIN_REDIRECT_URL = "hello"
LOGOUT_REDIRECT_URL = "thanks"
.vscode
src
|-core (app one)
|-drf_api (project folder)
|-_pycache_
|- __init__.py
|- settings.py
|- urls.py
|- views.py
|-wsgi.py
|-images
|-uploads
|-static
|-core
|-webportal
|-templates
|-base.html
|-hello.html
|-index.html
|-thanks.html
|-webportal (app two)
|-__pycache__
|-migrations
|-templates
|-webportal
|-login.html
|-signup.html
|-__init__.py
|-admin.py
|-apps.py
|-forms.py
|-models.py
|-tests.py
|-urls.py
|-views.py
|- .gitignore
|-manage.py
You are using the same path for django.contrib.auth.urls
and webportal.urls
.
That means webportal/login/
is matched by django.contrib.auth.urls
first, which uses the template registration/login.html
.
You can switch the order of the includes so that login is matched by the pattern in webportal.urls
first.
path("webportal/", include("webportal.urls", namespace="webportal")),
path("webportal/", include("django.contrib.auth.urls")),
Alternatively, since all you are changing is the template name, you could move the login template to webportal/templates/registration/login.html
, remove your URL pattern, and let django.contrib.auth.urls
handle logins.