2

I'm trying to create a usercreationform on the homepage of my website. After reading and watching tutorials on user creation I noticed everyone creates a separate HTML page for "signup", however, I want my signup page to be directly on my homepage - is this a possibility? I'm finding it difficult to understand with 'accounts' having its own separate app, as well as the homepage having its own app, which I have called mine 'game'. Do both apps have to be separate? Am I able to make the accounts app my main 'homepage' app?

Can anyone recommend any tutorials on this? I think I should also mention I'm quite new to django. Thank you.

My homepage app (titled game) urls.py:

from django.contrib import admin
from django.urls import path
from.import views

urlpatterns = [


    path('', views.game_start),
]

views.py:

from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
from .models import Game

def game_start(request):
    games = Game.objects.all().order_by('date') # grabs all records in game in db table, order by date
    return render (request, 'game/game_start.html', {'games':games})

def signup_view(request):
    form = UserCreationForm()
    return render(request, 'game/game_start.html', {'form': form})

accounts/urls.py:

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

app_name = 'accounts'

urlpatterns = [
    path('', game_views.game_start, name="home"),
]

accounts/views.py:

from django.http import HttpResponse
from django.shortcuts import render

def about(request):
    # return HttpResponse('Price is right game one')
    return render(request, 'about.html')
glumplum
  • 133
  • 7
Alexandra
  • 631
  • 1
  • 13
  • 24

2 Answers2

2

I want my signup page to be directly on my homepage - is this a possibility?

Yes it's a possibility that you can define a custom signup function in your accounts app and then import that inside of your homepage app like this:

accounts/views.py:

def signup(request):
    data = {'form':None, 'user_created': False}
    if request.method == 'POST':
       form = UserCreationForm(request.POST)
       if form.is_valid():
          user = form.save() 
          # do soemthing with the registered user 
          data['user_created'] = True
    else: 
       form = UserCreationForm() 
    data['form'] = form
    return data   

homepage/views.py:

from accounts.views import signup

def game_start(request):
    games = Game.objects.all().order_by('date') 
    data = {'msg': ''}
    response = signup(request)
    if response['user_created']:
       # you can redirect user here to somewhere else when they have been registered.
       data['msg'] = 'Thanks for being the part of our beautiful community!' 
    return render(request, 'game/game_start.html', {
                'games':games,
                'form': response['form'],
                'msg': data['msg']
    })

game_start.html:

<p>{{msg}}</p>
<form action="/" method="post">
    {% csrf_token %}
    {{ form }}
    <button type="submit">Sign Up</button>
</form>

Do both apps have to be separate?

Well, you can have both of them under one app but that is not recommended because of the following reasons:

  • You won't take advantage of App Reusability.
  • All of your code would look messy.
  • Debugging would be hard.

If you are having difficult understanding what apps in django are, then you can simply take a look at my answer here

it4Astuces
  • 432
  • 5
  • 17
Ahtisham
  • 9,170
  • 4
  • 43
  • 57
  • Is there a way I can use django's userCreationForm rather than a bootstrap form? I'm trying to broaden my studies and not use bootstrap so much. I'm hoping to stick with django. – Alexandra Feb 04 '19 at 04:20
  • @Alexandra You can use your own css class also. Take a look at my app form. [Form](https://github.com/manjurulhoque/django-fiverr-clone/blob/master/fiverrapp/templates/accounts/form.html) Use your CSS. Bootstrap isn't mandatory here. – Manzurul Hoque Rumi Feb 04 '19 at 06:06
  • 1
    @Alexandra I removed bootstraps and did everything with just django. Just you as wanted. :) – Ahtisham Feb 05 '19 at 02:04
  • Thanks, Ahtisham! I was able to get the contact form uploaded on my homepage. Many thanks! – Alexandra Feb 07 '19 at 02:38
  • @Ahtisham, I gave you credit in the provided link. Thanks, man. – Alexandra Feb 08 '19 at 02:12
0

You could include the form in your "game_start.html" template:

{% if not user.is_authenticated %}

  <form role="form"
         action="{% url 'player_login' %}"
         method="post">
    {% csrf_token %}
    <p>Please login.</p>

    {{ form.as_p }}

    <button type="submit">Sign in</button>
  </form>
  {% endif %}

This assumes you have a named url pattern player_login.

glumplum
  • 133
  • 7