dimanche 6 août 2017

How do I add users to database so I can login using credentials?

Okay, so here's the issue; I will try to make this as short-winded as possible:

I have gone through a tutorial at https://www.youtube.com/watch?v=CFypO_LNmcc and https://www.youtube.com/watch?v=Tam4IGrPESg to create a register, login, and logout system for my website/webapp.

I have successfully been able to create the pages necessary to register a user successfully. To prove that registered users have been created successfully I have monitored users through the /admin portal to confirm that the users were indeed created.

The way that I have things set up, after registration you must click on a link that takes you to the login page and login; however, every time I attempt to login using the newly created user I get redirected to my invalid_credentials.html although I have the password field set to expose my password so I can verify that the correct password is being typed.

Now, in this tutorial that I have followed, the person in the video is doing "python manage.py syncdb". I assume that this is to sync the newly created users to be able to login. For me, since I am using Django 1.11, I am typing "python manage.py makemigrations" and then "python manage.py migrate". The issue is that when I go to make migrations, my cmd is notifying me that it detects no changes whatsoever.

With all of this being said, I need some assistance guys. How do I add my users to my db so that I can login with registered credentials? Below I will give you my overall file setup as well as my urls.py and views.py code that has enabled what I have gotten so far.

FILE SETUP: "OBSweb" folder (main website folder that contains everything else)

"OBSweb" folder contains "mysite" folder

"mysite" folder contains "mysite" folder, "company" folder, db.sqlite3, and manage.py

"mysite" folder within "mysite" folder contains init.py, settings.py file, urls.py, and wsgi.py

"company" folder within "mysite" folder contains "migrations" folder, another "mysite" folder (with settings, urls.py; etc), "static" folder, "templates" folder, init.py, admin.py, apps.py, models.py, tests.py, urls.py, and views.py

"templates" folder within "company" folder within "mysite" folder contains "company" folder--> this folder holds all of my html files for render: compare.html, contact.html, header.html, home.html, invalid_login.html, learn.html, loggedin.html, login.html, logout.html, register.html, register_success.html, and services.html.

Here are the urls.py and views.py code that are within the main "company" folder that has enabled everything thus far:

urls.py

from django.conf.urls import url, include
from . import views


urlpatterns = [
    url(r'^$', views.home, name='home'),

    url(r'^home/', views.home, name='home'),
    url(r'^contact/', views.contact, name='contact'),
    url(r'^services/', views.services, name='services'),
    url(r'^compare/', views.compare, name='comparisons'),

    url(r'^accounts/login/$', views.login, name='login'),
    url(r'^accounts/auth/$', views.auth_view, name='auth'),
    url(r'^accounts/logout/$', views.logout, name='logout'),
    url(r'^accounts/loggedin/$', views.loggedin, name='loggedin'),
    url(r'^accounts/invalid/$', views.invalid_login, name='invalid_login'),
    url(r'^accounts/register/$', views.register_user, name='register'),
    url(r'^accounts/register_success/$', views.register_success,
        name='register_success'),

]

views.py

from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.template.context_processors import csrf
from django.contrib.auth.forms import UserCreationForm



def home(request):
    return render(request, 'company/home.html')

def contact(request):
    return render(request, 'company/contact.html')

def services(request):
    return render(request, 'company/services.html')

def compare(request):
    return render(request, 'company/compare.html')

def learn(request):
    return render(request, 'company/learn.html')

def login (request):
    c = {}
    c.update(csrf(request))
    return render_to_response('company/login.html', c)

def auth_view(request):
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        return HttpResponseRedirect('/accounts/loggedin')
    else:
        return HttpResponseRedirect('/accounts/invalid')

def loggedin(request):
    return render_to_response('company/loggedin.html' ,
                          {'full_name': request.user.username})

def invalid_login(request):
    return render_to_response('company/invalid_login.html')

def logout(request):
    auth.logout(request)
    return render_to_response('company/logout.html')

def register_user(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/register_success')

    args = {}
    args.update(csrf(request))

    args['form'] = UserCreationForm()
    return render_to_response('company/register.html', args)

def register_success(request):
    return render_to_response('company/register_success.html')

I know that posting all of this may have not been necessary, but because I can't even begin to pinpoint what the problem is I thought I'd try to be as detailed as possible about my problem and setup.

To reiterate, I am having no issues in registering a new user and it being reflected within the admin portal. The Issue I am having is logging into the login.html successfully with the new registered credentials. When I try to migrate any changes, no changes are detected although it is shown that a new user is created when I view "users" in the admin portal. Any help would be GREATLY appreciated. Thanks guys/girls.

Aucun commentaire:

Enregistrer un commentaire