Hi I am trying to make a register link in my html-file. But it only works when I am already logged in.
settings.py
AUTH_USER_MODEL = 'users.User'
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.urls import reverse_lazy
class User(AbstractUser):
# Address
plz = models.CharField(max_length=5)
ort = models.CharField(max_length=30)
strasse = models.CharField(max_length=30)
strnr = models.CharField(max_length=5)
# Kontakt
mobil = models.CharField(max_length=20)
forms.py
I customized the original UserCreationForm to implement to additional fields:
class UserCreate(forms.ModelForm):
--snip--
class Meta:
model = User
fields = ('username', 'password1', 'password2', 'first_name',
'last_name', 'email', 'mobil', 'plz', 'ort',
'strasse', 'strnr',)
views.py
class UserCreate(CreateView):
model = User
form_class = UserCreate
template_url_suffix = '_create_form'
def get_context_data(self, **kwargs):
context = super(UserCreate, self).get_context_data(**kwargs)
context['head'] = 'Account erstellen'
return context
urls.py
url(r'^user/create/$', views.UserCreate.as_view(), name='user-create'),
If I am logged in, I can use the link to create a user instance:
base.html
<body>
<ul>
<li><a href="{% url 'users:home' %}">Home</a></li>
<li><a href="{% url 'users:user-create' %}">register</a></li>
{% if user.is_authenticated %}
<li><a href="{% url 'users:user-detail' user.id %}">Account</a></li>
{% if user.sportler %}
<li><a href="{% url 'users:sportler-detail' user.sportler.id %}">Sportler</a></li>
{% else %}
<li><a href="{% url 'users:sportler-create' %}">Sportler werden</a></li>
{% endif %}
....
How can I make it work, if one is not logged in???
Error: Reverse for 'user-detail' with arguments '(None,)' not found. 1 pattern(s) tried: ['user/(?P[0-9]+)/detail/$']