Browse Source

add authentication via django auth package

Gesche 1 year ago
parent
commit
5d618dc16e

+ 5
- 0
accounts/static/css/styles.css View File

@@ -0,0 +1,5 @@
1
+.sidebar-nav {
2
+    margin-top: 20px;
3
+    padding: 0;
4
+    list-style: none;
5
+}

+ 33
- 0
accounts/templates/base_generic.html View File

@@ -0,0 +1,33 @@
1
+<!DOCTYPE html>
2
+<html lang="en">
3
+<head>
4
+  {% block title %}<title>Ambitious Server</title>{% endblock %}
5
+  <meta name="viewport" content="width=device-width, initial-scale=1">
6
+  <!--link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"-->
7
+  <!-- Add additional CSS in static file -->
8
+  {% load static %}
9
+  <link rel="stylesheet" href="{% static 'css/styles.css' %}">
10
+</head>
11
+<body>
12
+  <div class="container-fluid">
13
+    <div class="row">
14
+      <div class="col-sm-2">
15
+      {% block sidebar %}
16
+        <ul class="sidebar-nav">
17
+        {% if user.is_authenticated %}
18
+          <li>User: {{ user.get_username }}</li>
19
+          <li><a href="{% url 'logout'%}?next={{request.path}}">Logout</a></li>
20
+        {% else %}
21
+          <li><a href="{% url 'login'%}?next={{request.path}}">Login</a></li>
22
+        {% endif %}
23
+          <li><a href="{% url 'accounts:index' %}">Home</a></li>
24
+          <li><a href="">Account</a></li>
25
+          <li><a href="">All authors</a></li>
26
+        </ul>
27
+     {% endblock %}
28
+      </div>
29
+      <div class="col-sm-10 ">{% block content %}{% endblock %}</div>
30
+    </div>
31
+  </div>
32
+</body>
33
+</html>

+ 4
- 10
accounts/templates/index.html View File

@@ -1,10 +1,6 @@
1
-<!DOCTYPE html>
2
-<html lang="en">
3
-<head>
4
-    <meta charset="UTF-8">
5
-    <title>Highscore</title>
6
-</head>
7
-<body>
1
+{% extends "base_generic.html" %}
2
+
3
+{% block content %}
8 4
 <h1>Highscore</h1>
9 5
 
10 6
 {% if best_players %}
@@ -25,7 +21,5 @@
25 21
 {% endif %}
26 22
 
27 23
 <a href="{% url 'accounts:register' %}">Register now!</a>
28
-<a href="{% url 'accounts:login' %}">Login</a>
29 24
 
30
-</body>
31
-</html>
25
+{% endblock %}

+ 4
- 9
accounts/templates/login.html View File

@@ -1,10 +1,6 @@
1
-<!DOCTYPE html>
2
-<html lang="en">
3
-<head>
4
-    <meta charset="UTF-8">
5
-    <title>Login</title>
6
-</head>
7
-<body>
1
+{% extends "base_generic.html" %}
2
+
3
+{% block content %}
8 4
 <h1>Login</h1>
9 5
 
10 6
 <form method="post">
@@ -13,5 +9,4 @@
13 9
   <button type="submit">Login</button>
14 10
 </form>
15 11
 
16
-</body>
17
-</html>
12
+{% endblock %}

+ 6
- 0
accounts/templates/registration/logged_out.html View File

@@ -0,0 +1,6 @@
1
+{% extends "base_generic.html" %}
2
+
3
+{% block content %}
4
+  <p>Logged out!</p>  
5
+  <a href="{% url 'login'%}">Click here to login again.</a>
6
+{% endblock %}

+ 40
- 0
accounts/templates/registration/login.html View File

@@ -0,0 +1,40 @@
1
+{% extends "base_generic.html" %}
2
+
3
+{% block content %}
4
+
5
+{% if form.errors %}
6
+  <p>Your username and password didn't match. Please try again.</p>
7
+{% endif %}
8
+
9
+{% if next %}
10
+  {% if user.is_authenticated %}
11
+    <p>Your account doesn't have access to this page. To proceed,
12
+    please login with an account that has access.</p>
13
+  {% else %}
14
+    <p>Please login to see this page.</p>
15
+  {% endif %}
16
+{% endif %}
17
+
18
+<form method="post" action="{% url 'login' %}">
19
+{% csrf_token %}
20
+<table>
21
+
22
+<tr>
23
+  <td>{{ form.username.label_tag }}</td>
24
+  <td>{{ form.username }}</td>
25
+</tr>
26
+
27
+<tr>
28
+  <td>{{ form.password.label_tag }}</td>
29
+  <td>{{ form.password }}</td>
30
+</tr>
31
+</table>
32
+
33
+<input type="submit" value="login" />
34
+<input type="hidden" name="next" value="{{ next }}" />
35
+</form>
36
+
37
+{# Assumes you setup the password_reset view in your URLconf #}
38
+<p><a href="{% url 'password_reset' %}">Lost password?</a></p>
39
+
40
+{% endblock %}

+ 6
- 0
accounts/templates/registration/password_reset_complete.html View File

@@ -0,0 +1,6 @@
1
+{% extends "base_generic.html" %}
2
+
3
+{% block content %}
4
+  <h1>The password has been changed!</h1>
5
+  <p><a href="{% url 'login' %}">log in again?</a></p>
6
+{% endblock %}

+ 29
- 0
accounts/templates/registration/password_reset_confirm.html View File

@@ -0,0 +1,29 @@
1
+{% extends "base_generic.html" %}
2
+
3
+{% block content %}
4
+    {% if validlink %}
5
+        <p>Please enter (and confirm) your new password.</p>
6
+        <form action="" method="post">
7
+        {% csrf_token %}
8
+            <table>
9
+                <tr>
10
+                    <td>{{ form.new_password1.errors }}
11
+                        <label for="id_new_password1">New password:</label></td>
12
+                    <td>{{ form.new_password1 }}</td>
13
+                </tr>
14
+                <tr>
15
+                    <td>{{ form.new_password2.errors }}
16
+                        <label for="id_new_password2">Confirm password:</label></td>
17
+                    <td>{{ form.new_password2 }}</td>
18
+                </tr>
19
+                <tr>
20
+                    <td></td>
21
+                    <td><input type="submit" value="Change my password" /></td>
22
+                </tr>
23
+            </table>
24
+        </form>
25
+    {% else %}
26
+        <h1>Password reset failed</h1>
27
+        <p>The password reset link was invalid, possibly because it has already been used. Please request a new password reset.</p>
28
+    {% endif %}
29
+{% endblock %}

+ 5
- 0
accounts/templates/registration/password_reset_done.html View File

@@ -0,0 +1,5 @@
1
+{% extends "base_generic.html" %}
2
+
3
+{% block content %}
4
+  <p>We've emailed you instructions for setting your password. If they haven't arrived in a few minutes, check your spam folder.</p>
5
+{% endblock %}

+ 2
- 0
accounts/templates/registration/password_reset_email.html View File

@@ -0,0 +1,2 @@
1
+Someone asked for password reset for email {{ email }}. Follow the link below:
2
+{{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}

+ 12
- 0
accounts/templates/registration/password_reset_form.html View File

@@ -0,0 +1,12 @@
1
+{% extends "base_generic.html" %}
2
+
3
+{% block content %}
4
+  <form action="" method="post">
5
+  {% csrf_token %}
6
+  {% if form.email.errors %}
7
+    {{ form.email.errors }}
8
+  {% endif %}
9
+      <p>{{ form.email }}</p> 
10
+    <input type="submit" class="btn btn-default btn-lg" value="Reset password">
11
+  </form>
12
+{% endblock %}

+ 1
- 1
accounts/views.py View File

@@ -30,4 +30,4 @@ class IndexView(generic.ListView):
30 30
     context_object_name = 'best_players'
31 31
 
32 32
     def get_queryset(self):
33
-        return Players.objects.filter(group_id=1).order_by('-level')[:10]
33
+        return Players.objects.filter(group_id=1).order_by('-level').values('name', 'level')[:10]

+ 5
- 1
tibia_website/settings.py View File

@@ -55,7 +55,7 @@ ROOT_URLCONF = 'tibia_website.urls'
55 55
 TEMPLATES = [
56 56
     {
57 57
         'BACKEND': 'django.template.backends.django.DjangoTemplates',
58
-        'DIRS': [],
58
+        'DIRS': [os.path.join(BASE_DIR, 'templates')],
59 59
         'APP_DIRS': True,
60 60
         'OPTIONS': {
61 61
             'context_processors': [
@@ -123,3 +123,7 @@ USE_TZ = True
123 123
 # https://docs.djangoproject.com/en/2.1/howto/static-files/
124 124
 
125 125
 STATIC_URL = '/static/'
126
+
127
+# Redirect to home URL after login (Default redirects to /accounts/profile/)
128
+LOGIN_REDIRECT_URL = '/'
129
+

+ 2
- 1
tibia_website/urls.py View File

@@ -18,5 +18,6 @@ from django.urls import path, include
18 18
 
19 19
 urlpatterns = [
20 20
     path('admin/', admin.site.urls),
21
-    path('accounts/', include('accounts.urls') )
21
+    path('accounts/', include('accounts.urls')),
22
+    path('auth/', include('django.contrib.auth.urls')),
22 23
 ]

Loading…
Cancel
Save