0

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/$']

  • Post you full html or atleast `user-detail` using part – itzMEonTV May 29 '17 at 12:53
  • User-Detail is not supposed to be called. Not when creating or searching for a success_url. –  May 29 '17 at 13:27
  • What is user there? Try request.user instead of it – itzMEonTV May 29 '17 at 14:05
  • 1
    This question has absolutely nothing at all to do with it's title. – e4c5 May 29 '17 at 14:26
  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](https://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – e4c5 May 29 '17 at 14:27
  • Well, I dont know what to look for. When I am logged in with a user. I can load the "user-create"-view and create a new user. I can log out and then log in with the new user. If I am not logged in, I get the above error. I dont get why the system even tries to load the user-detail. I also removed any decorators. –  May 29 '17 at 16:12
  • I tried 'request.user.id' instead of user.id inAccount - Reverse for 'user-detail' with arguments '(None,)' not found. None is because nobody is logged in. I dont get why it wants to load the user-detail at all. –  May 29 '17 at 16:28

1 Answers1

0

I had a back-link in my creation form. Call me morron.

<a href="{% url 'users:user-detail' user.id %}"><button>Zurück</button></a>

Edited it to <a href="{% url 'users:home' %}"><button>Zurück</button></a> and it works...