Monday, May 29, 2017

How to render a HTML template in App2 from App1

Dear Experts, Please can you help me with this issue. I spent a lot of time on this but not able to solve this basic issue. My need is as below.

Sincere apologies if I am asking for too much in that you have to read this whole code.

Please try and help me.

I have a project called ella that has to apps called elah & adm1n

elah app has a index.html that has a link to login.html. Once the user enters the right credentials, I check his role and either take him to adm1n app / dashboard html or take him to a 3rd app called employee (yet to build)

The issue I have is after the login is validated, the adm1n/dashboard.html is rendered but the url does not change and remains http://127.0.0.1:8000/elah/login/. This is from where the new template is called. Also i put a hyperlink to render another html in the adm1n app. On click of this app, the login html is rendered.

What am I doing wrong here?

ella
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import RedirectView

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^elah/', include('elah.urls')),
    url(r'^adm1n/', include('adm1n.urls')),
    url(r'^$', RedirectView.as_view(url='/elah/', permanent=True)),
]

elah.urls
from django.conf.urls import url
from elah import views

app_name = 'elah'
urlpatterns = [
    url(r'^$', views.elah, name='elah'),
    url(r'^login/', views.login, name='login'),
    url(r'^forgotpassword/', views.forgotpassword, name='forgotpassword'),
    url(r'^resetpassword/', views.resetpassword, name='resetpassword'),
]

elah.views
from django.shortcuts import render
from django.contrib import messages
from elah.models import users
from django.core.mail import send_mail
from django.template import Context
from django.template.loader import get_template
from django.core.mail import EmailMessage

from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags

def elah(request):
    return render(request, 'elah/index.html')

def login(request):
    lReturnMessage, lValid, lEmailIdE = '', '', ''
    if request.method == 'POST':
        lEmailId = request.POST.get("emailid")
        lPassword = request.POST.get("password")
        lReturnMessage = 'You Are A Valid User'
        lValid = 'Y'
        try:
            tFetchData = users.objects.get(emailid = lEmailId)
        except users.DoesNotExist:
            lReturnMessage = str(lEmailId) + ' is not a valid Email ID. Re-enter the correct Email ID. Contact your Administrator to check if your account has been setup.'
            lValid = 'N'
            lEmailIdE = lEmailId
        if lValid == 'Y':
            if lPassword != tFetchData.password:
                lReturnMessage = 'Incorrect Password. Enter the correct password. Use Forgot Password if you do not remember your password.'
                lValid = 'N'
                lEmailIdE = lEmailId
            if lValid == 'Y':
                return render(request, 'adm1n/dashboard.html')

    return render(request, 'elah/login.html', {'lReturnMessage': lReturnMessage, 'lValid': lValid, 'lEmailIdE': lEmailIdE})


def forgotpassword(request):
    lReturnMessage, lValid, lEmailIdE = '', '', ''
    if request.method == 'POST':
        lEmailId = request.POST.get("emailid")
        lReturnMessage = 'Your Login credentials have been emailed to ' + lEmailId + '  ... Click on Return To Login and use the credential sent to login'
        lValid = 'Y'
        try:
            tFetchData = users.objects.get(emailid = lEmailId)
        except users.DoesNotExist:
            lReturnMessage = lEmailId + ' is not a valid Email ID. Re-enter the correct Email ID. Contact your Administrator to check if your account has been setup.'
            lValid = 'N'
            lEmailIdE = lEmailId
        if lValid == 'Y':
            subject = "I am an HTML email"
            to = ['yeddu.prasad@outlook.com']
            from_email = ''
            message = render_to_string('elah/emailalert.html').strip()
            msg = EmailMultiAlternatives(subject, message, to=to, from_email=from_email)
            msg.content_subtype = 'html'
            msg.send()
    return render(request, 'elah/forgotpassword.html', {'lReturnMessage': lReturnMessage, 'lValid': lValid, 'lEmailIdE': lEmailIdE})


def resetpassword(request):
    lReturnMessage, lValid, lEmailIdE, lOldPasswordE = '', '', '', ''
    if request.method == 'POST':
        lEmailId = request.POST.get("emailid")
        lOldPassword = request.POST.get("oldpassword")
        lNewPassword = request.POST.get("newpassword")
        lReenterPassowrd = request.POST.get("reenterpassowrd")
        lReturnMessage = 'Your password has been updated and emailed to ' + lEmailId + '  ... Click on Return To Login and use the credential sent to login'
        lValid = 'Y'
        try:
            tFetchData = users.objects.get(emailid = lEmailId)
        except users.DoesNotExist:
            lReturnMessage = lEmailId + ' is not a valid Email ID. Re-enter the correct Email ID. Contact your Administrator to check if your account has been setup.'
            lValid = 'N'
            lEmailIdE = lEmailId
        if lValid == 'Y':
            if lOldPassword != tFetchData.password:
                lReturnMessage = 'The Current Password you entered is not valid. Re-enter the right password.'
                lValid = 'N'
                lEmailIdE = lEmailId
        if lValid == 'Y':
            if lNewPassword != lReenterPassowrd:
                lReturnMessage = 'Your New Password and Confirmation Password Do not Match.'
                lValid = 'N'
                lEmailIdE = lEmailId
                lOldPasswordE = lOldPassword
    return render(request, 'elah/resetpassword.html',  {'lReturnMessage': lReturnMessage, 'lValid': lValid, 'lEmailIdE': lEmailIdE, 'lOldPasswordE': lOldPasswordE})

adm1n.urls
from django.conf.urls import url
from adm1n import views

app_name = 'adm1n'
urlpatterns = [
    url(r'^dashboard', views.dashboard, name='dashboard'),
    url(r'^dashboard1', views.dashboard1, name='dashboard1'),
]

adm1n.views
from django.shortcuts import render

def dashboard(request):
    return render(request, 'adm1n/dashboard.html')

def dashboard1(request):
    return render(request, 'adm1n/dashboard1.html')


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/404a4124-4855-44bd-ac9a-72798696a21c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment