A Django app that allows visitors to interact with your site as a guest user without requiring registration.

Overview

django-guest-user

A Django app that allows visitors to interact with your site as a guest user without requiring registration.

Largely inspired by django-lazysignup and rewritten for Django 3 and Python 3.6 and up.

Installation

Install the package with your favorite package manager from PyPI:

pip install django-guest-user

Add the app to your INSTALLED_APPS and AUTHENTICATION_BACKENDS:

INSTALLED_APPS = [
    ...
    "django_guest_user",
]

AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",
    "guest_user.backends.GuestBackend",
]

Add the patterns to your URL config:

urlpatterns = [
    ...
    path("convert/", include("guest_user.urls")),
]

Don't forget to run migrations:

python manage.py migrate

How to use

Guest users are not created for every unauthenticated request. Instead, use the @allow_guest_user decorator on a view to enable that view to be accessed by a temporary guest user.

from guest_user.decorators import allow_guest_user

@allow_guest_user
def my_view(request):
    # Will always be either a registered a guest user.
    username = request.user.username
    return HttpResponse(f"Hello, {username}!")

API

@guest_user.decorators.allow_guest_user

View decorator that will create a temporary guest user in the event that the decorated view is accessed by an unauthenticated visitor.

Takes no arguments.

@guest_user.decorators.guest_user_required(redirect_field_name="next", login_url=None)

View decorator that redirects to a given URL if the accessing user is anonymous or already authenticated.

Arguments:

  • redirect_field_name: URL query parameter to use to link back in the case of a redirect to the login url. Defaults to django.contrib.auth.REDIRECT_FIELD_NAME ("next").
  • login_url: URL to redirect to if the user is not authenticated. Defaults to the LOGIN_URL setting.

@guest_user.decorators.regular_user_required(redirect_field_name="next", login_url=None)

Decorator that will not allow guest users to access the view. Will redirect to the conversion page to allow a guest user to fully register.

Arguments:

  • redirect_field_name: URL query parameter to use to link back in the case of a redirect to the login url. Defaults to django.contrib.auth.REDIRECT_FIELD_NAME ("next").
  • login_url: URL to redirect to if the user is a guest. Defaults to "guest_user_convert".

guest_user.functions.get_guest_model()

The guest user model is swappable. This function will return the currently configured model class.

guest_user.functions.is_guest_user(user)

Check wether the given user instance is a temporary guest.

guest_user.signals.converted

Signal that is dispatched when a guest user is converted to a regular user.

Template tag is_guest_user

A filter to use in templates to check if the user object is a guest.

{% load guest_user %}

{% if user|is_guest_user %}
  Hello guest.
{% endif %}

Settings

Various settings are provided to allow customization of the guest user behavior.

GUEST_USER_ENABLED

bool. If False, the @allow_guest_user decorator will not create guest users. Defaults to True.

GUEST_USER_MODEL

str. The swappable model identifier to use as the guest model. Defaults to "guest_user.Guest".

GUEST_USER_NAME_GENERATOR

str. Import path to a function that will generate a username for a guest user. Defaults to "guest_user.functions.generate_uuid_username".

Included with the package are two alternatives:

"guest_user.functions.generate_numbered_username": Will create a random four digit number prefixed by GUEST_USER_NAME_PREFIX.

"guest_user.functions.generate_friendly_username": Creates a friendly and easy to remember username by combining an adjective, noun and number. Requires random_username to be installed.

GUEST_USER_NAME_PREFIX

str. A prefix to use with the generate_numbered_username generator. Defaults to "Guest".

GUEST_USER_CONVERT_FORM

str. Import path for the guest conversion form. Must implement get_credentials to be passed to Django's authenticate function. Defaults to "guest_user.forms.UserCreationForm".

GUEST_USER_CONVERT_PREFILL_USERNAME

bool. Set the generated username as initial value on the conversion form. Defaults to False.

GUEST_USER_CONVERT_URL

str. URL name for the convert view. Defaults to "guest_user_convert".

GUEST_USER_CONVERT_REDIRECT_URL

str. URL name to redirect to after conversion, unless a redirect parameter was provided. Defaults to "guest_user_convert_success".

GUEST_USER_BLOCKED_USER_AGENTS

list[str]. Web crawlers and other user agents to block from becoming guest users. The list will be combined into a regular expression. Default includes a number of well known bots and spiders.

Status

This project is currently untested. But thanks to previous work it is largely functional.

I decided to rewrite the project since the original project hasn't seen any larger updates for a few years now and the code base was written a long time ago.

You might also like...
 An insecure login and registration website with Django.
An insecure login and registration website with Django.

An insecure login and registration website with Django.

Vehicle registration using Python, Django and SQlite3

PythonCrud Cadastro de veículos utilizando Python, Django e SQlite3 Para acessar o deploy no Heroku:

A handy tool for generating Django-based backend projects without coding. On the other hand, it is a code generator of the Django framework.
A handy tool for generating Django-based backend projects without coding. On the other hand, it is a code generator of the Django framework.

Django Sage Painless The django-sage-painless is a valuable package based on Django Web Framework & Django Rest Framework for high-level and rapid web

Django API without Django REST framework.

Django API without DRF This is a API project made with Django, and without Django REST framework. This project was done with: Python 3.9.8 Django 3.2.

django-idom allows Django to integrate with IDOM

django-idom allows Django to integrate with IDOM, a package inspired by ReactJS for creating responsive web interfaces in pure Python.

Displaying objects on maps in the Django views and administration site.
Displaying objects on maps in the Django views and administration site.

DjangoAdminGeomap library The free, open-source DjangoAdminGeomap library is designed to display objects on the map in the Django views and admin site

Use webpack to generate your static bundles without django's staticfiles or opaque wrappers.

django-webpack-loader Use webpack to generate your static bundles without django's staticfiles or opaque wrappers. Django webpack loader consumes the

A Django web application that allows you to be in the loop about everything happening in your neighborhood.
A Django web application that allows you to be in the loop about everything happening in your neighborhood.

A Django web application that allows you to be in the loop about everything happening in your neighborhood. From contact information of different handyman to meeting announcements or even alerts.

Django project starter on steroids: quickly create a Django app AND generate source code for data models + REST/GraphQL APIs (the generated code is auto-linted and has 100% test coverage).

Create Django App 💛 We're a Django project starter on steroids! One-line command to create a Django app with all the dependencies auto-installed AND

Comments
  • Add django-tos contrib app

    Add django-tos contrib app

    This PR adds an integration with django-tos.

    It is necessary when using django-tos's middleware option (2). When using django-tos' middleware, since guest users have full user accounts they are required to agree to the site's terms of service before they can do anything else.

    I have refactored django-tos a bit in revsys/django-tos#65 specifically to make this custom middleware easier to write. That PR allows us to minimize the amount of code necessary in this middleware and any changes to upstream django-tos should not interfere with this middleware.

    opened by blag 3
  • RFC on a changing GuestManager.convert to update in-place

    RFC on a changing GuestManager.convert to update in-place

    Hi @julianwachholz,

    First off, thank you very much for maintaining this package for new versions of Django!

    I've opened this ticket to propose a relatively simple change (or addition) of the GuestManager in the package that I think would enable greater functionality for devs wanting to create guest users.

    Background

    Business Use Case

    I started using this package to enable guest users functionality on a site, with the goal that:

    • ephemeral guest users would be able to modify the db (ie CRUD their own objects like a regular user)
    • after conversion all of the state in related models would be maintained

    After reading the documentation for django-guest-user I didn't see any flags that stated this usage wasn't possible, but after installing the package and trying to use it, to the best of my understanding, the use case above is not supported. I think this has also been raised previously in https://github.com/julianwachholz/django-guest-user/issues/3.

    EDIT:

    • This use case is supported; I had a misconfigured form and had misread the guest creation workflow when debugging.

    Thanks very much for your time!

    opened by mbhynes 3
  • How to log in instead of registering a new user?

    How to log in instead of registering a new user?

    Hi @julianwachholz,

    Thanks a lot for working on this package, it's very useful!

    I had a question I hoped you could help me answer: Is there a way to allow the user to log in, instead of creating a new user during the conversion? So that if a user hadn't logged in before putting items in the cart, he can still keep them

    Thank you.

    Best, Dylan

    opened by dylanjcastillo 2
  • Django 4.0

    Django 4.0

    This PR:

    • removes one last feature that was deprecated in Django 4.0 (which silences a warning from this package when running tests)
    • adds Django 4.0 to the test matrix
    • removes Django 3.1 from the test matrix (since this is EOL in just a few days)
    • adds Python 3.10 to the test matrix
    opened by blag 1
Releases(0.5.3)
Owner
Julian Wachholz
Python and JavaScript developer. I enjoy riding motorcycles and playing board games. PGP: https://julianwachholz.dev/julian_wachholz_pub.asc
Julian Wachholz
Django-Audiofield is a simple app that allows Audio files upload, management and conversion to different audio format (mp3, wav & ogg), which also makes it easy to play audio files into your Django application.

Django-Audiofield Description: Django Audio Management Tools Maintainer: Areski Contributors: list of contributors Django-Audiofield is a simple app t

Areski Belaid 167 Nov 10, 2022
Coltrane - A simple content site framework that harnesses the power of Django without the hassle.

coltrane A simple content site framework that harnesses the power of Django without the hassle. Features Can be a standalone static site or added to I

Adam Hill 58 Jan 2, 2023
Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project

Django URL Shortener Django URL Shortener is a Django app to to include URL Shortening feature in your Django Project Install this package to your Dja

Rishav Sinha 4 Nov 18, 2021
Django-shared-app-isolated-databases-example - Django - Shared App & Isolated Databases

Django - Shared App & Isolated Databases An app that demonstrates the implementa

Ajai Danial 5 Jun 27, 2022
Django-environ allows you to utilize 12factor inspired environment variables to configure your Django application.

Django-environ django-environ allows you to use Twelve-factor methodology to configure your Django application with environment variables. import envi

Daniele Faraglia 2.7k Jan 7, 2023
pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-django allows you to test your Django project/applications with the pytest testing tool.

pytest-dev 1.1k Dec 14, 2022
A web app which allows user to query the weather info of any place in the world

weather-app This is a web app which allows user to get the weather info of any place in the world as soon as possible. It makes use of OpenWeatherMap

Oladipo Adesiyan 3 Sep 20, 2021
Simple yet powerful and really extendable application for managing a blog within your Django Web site.

Django Blog Zinnia Simple yet powerful and really extendable application for managing a blog within your Django Web site. Zinnia has been made for pub

Julien Fache 2.1k Dec 24, 2022
A Django app to initialize Sentry client for your Django applications

Dj_sentry This Django application intialize Sentry SDK to your Django application. How to install You can install this packaging by using: pip install

Gandi 1 Dec 9, 2021
An app that allows you to add recipes from the dashboard made using DJango, JQuery, JScript and HTMl.

An app that allows you to add recipes from the dashboard. Then visitors filter based on different categories also each ingredient has a unique page with their related recipes.

Pablo Sagredo 1 Jan 31, 2022