-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
225 additions
and
21 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class AccountsConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'accounts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.contrib.auth.models import AbstractUser | ||
from django.forms import ModelForm | ||
|
||
|
||
class UserForm(ModelForm): | ||
class Meta: | ||
model = AbstractUser | ||
fields = ('username', 'password') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.urls import reverse | ||
|
||
from menu import Menu, MenuItem | ||
|
||
|
||
def profile_title(request): | ||
"""Return a personalized title for our profile menu item | ||
""" | ||
# we don't need to check if the user is authenticated because our menu | ||
# item will have a check that does that for us | ||
name = request.user.get_full_name() or request.user | ||
|
||
return f"{name}'s Profile" | ||
|
||
|
||
# this item will be shown to users who are not logged in | ||
Menu.add_item("user", MenuItem("Sign in", | ||
reverse('accounts:sign_in'), | ||
icon='box-arrow-in-right', | ||
check=lambda r: not r.user.is_authenticated)) | ||
|
||
# this will only be shown to logged in users and also demonstrates how to use | ||
# a callable for the title to return a customized title for each request | ||
Menu.add_item("user", MenuItem(profile_title, | ||
reverse('accounts:profile'), | ||
icon='person-circle', | ||
check=lambda r: r.user.is_authenticated)) | ||
|
||
# this only shows to superusers | ||
Menu.add_item("user", MenuItem("Administration", | ||
reverse("admin:index"), | ||
icon='shield-lock', | ||
separator=True, | ||
check=lambda r: r.user.is_superuser)) | ||
|
||
Menu.add_item("user", MenuItem("Sign out", | ||
reverse('accounts:sign_out'), | ||
icon='box-arrow-right', | ||
check=lambda r: r.user.is_authenticated)) | ||
|
||
Menu.add_item("main", MenuItem("GitHub", | ||
"https://github.com/jazzband/django-simple-menu", | ||
icon="github")) | ||
Menu.add_item("main", MenuItem("Docs", | ||
"https://django-simple-menu.readthedocs.io/", | ||
icon="journal-code")) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{% extends 'base.html' %} | ||
|
||
{% block title %} | ||
Accounts | {{ block.super }} | ||
{% endblock %} | ||
|
||
{% block body %} | ||
<main class="py-5"> | ||
<div class="container"> | ||
<h1>Welcome!</h1> | ||
<p> | ||
This is an example website for | ||
<a href="https://github.com/jazzband/django-simple-menu">django-simple-menu</a>. | ||
</p> | ||
<p> | ||
Create a superuser and a normal user, then try to sign in with | ||
those to see, how the menu works. | ||
</p> | ||
</div> | ||
</main> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{% extends 'base.html' %} | ||
|
||
{% block title %} | ||
Your profile | {{ block.super }} | ||
{% endblock %} | ||
|
||
{% block body %} | ||
<main class="py-5"> | ||
<div class="container"> | ||
<h1>{{ request.user.get_full_name|default:"" }} | ||
(@{{ request.user.username }})</h1> | ||
<p> | ||
<strong>E-Mail:</strong> {{ request.user.email }} | ||
</p> | ||
</div> | ||
</main> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{% extends 'base.html' %} | ||
|
||
{% block title %} | ||
Sign in | {{ block.super }} | ||
{% endblock %} | ||
|
||
{% block body %} | ||
<main class="p-4"> | ||
<div class="container w-25 mx-auto"> | ||
<form action="{% url 'accounts:sign_in' %}" method="post"> | ||
{% csrf_token %} | ||
<h1>Sign in</h1> | ||
|
||
<div class="mb-3"> | ||
<label for="username">Username</label> | ||
<input type="text" class="form-control" id="username" | ||
name="username"> | ||
</div> | ||
<div class="mb-3"> | ||
<label for="password">Password</label> | ||
<input type="password" class="form-control" | ||
id="password" name="password"> | ||
</div> | ||
|
||
<button class="btn btn-lg btn-primary" type="submit"> | ||
Sign in | ||
</button> | ||
</form> | ||
</div> | ||
</main> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from django.urls import path | ||
|
||
from accounts import views | ||
|
||
app_name = 'accounts' | ||
urlpatterns = [ | ||
path('sign_in/', views.SignInView.as_view(), name='sign_in'), | ||
path('sign_out/', views.SignOutView.as_view(), name='sign_out'), | ||
|
||
path('', views.IndexView.as_view(), name='index'), | ||
path('profile/', views.ProfileView.as_view(), name='profile'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.contrib.auth.views import LoginView, LogoutView | ||
from django.urls import reverse_lazy | ||
from django.views.generic import TemplateView | ||
|
||
|
||
class SignInView(LoginView): | ||
template_name = 'accounts/sign_in.html' | ||
|
||
def get_success_url(self): | ||
return reverse_lazy('accounts:profile') | ||
|
||
|
||
class SignOutView(LogoutView): | ||
next_page = 'accounts:index' | ||
|
||
|
||
class IndexView(TemplateView): | ||
template_name = 'accounts/index.html' | ||
|
||
|
||
class ProfileView(TemplateView): | ||
template_name = 'accounts/profile.html' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,10 @@ | ||
"""example URL Configuration | ||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/4.0/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Import the include() function: from django.urls import include, path | ||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
""" | ||
from django.contrib import admin | ||
from django.urls import path | ||
from django.urls import include, path | ||
|
||
urlpatterns = [ | ||
path('admin/', admin.site.urls), | ||
|
||
path('', include('accounts.urls')), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{% load menu %} | ||
{% generate_menu %} | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>{% block title %}django-simple-menu example{% endblock %}</title> | ||
|
||
<link rel="stylesheet" | ||
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" | ||
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" | ||
crossorigin="anonymous" | ||
> | ||
<link rel="stylesheet" | ||
href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css" | ||
> | ||
|
||
|
||
{% block extra_head %}{% endblock extra_head %} | ||
</head> | ||
<body> | ||
<header> | ||
<nav class="navbar navbar-expand navbar-light bg-light"> | ||
<div class="container-fluid"> | ||
<a class="navbar-brand" href="{% url 'accounts:index' %}">dsm | ||
example app</a> | ||
{% with menu=menus.main %} | ||
{% include 'menu.html' %} | ||
{% endwith %} | ||
{% with menu=menus.user navbar_class="ms-auto" %} | ||
{% include 'menu.html' %} | ||
{% endwith %} | ||
|
||
</div> | ||
</nav> | ||
</header> | ||
{% block body %}{% endblock body %} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<ul class="navbar-nav {{ navbar_class|default:"" }}"> | ||
{% for item in menu %} | ||
<li class="nav-item"> | ||
<a class="nav-link {% if item.selected %}active{% endif %}" | ||
href="{{ item.url }}"> | ||
{% if item.icon %} | ||
<i class="bi bi-{{ item.icon }}"></i> | ||
{% endif %} | ||
{{ item.title }} | ||
</a> | ||
</li> | ||
{% endfor %} | ||
</ul> |