Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'

Tags:

python

django

I've created a custom user model in Django and everything went fine, but when I try to create a new user it gives me an error.

Here are my files :

models.py, where I created the user table:

class UserModelManager(BaseUserManager):
    def create_user(self, email, password, pseudo):
        user = self.model()
        user.name = name
        user.email = self.normalize_email(email=email)
        user.set_password(password)
        user.save()

        return user

    def create_superuser(self, email, password):
        '''
        Used for: python manage.py createsuperuser
        '''
        user = self.model()
        user.name = 'admin-yeah'
        user.email = self.normalize_email(email=email)
        user.set_password(password)

        user.is_staff = True
        user.is_superuser = True
        user.save()

        return user


class UserModel(AbstractBaseUser, PermissionsMixin):
    ## Personnal fields.
    email = models.EmailField(max_length=254, unique=True)
    name = models.CharField(max_length=16)
    ## [...]

    ## Django manage fields.
    date_joined = models.DateTimeField(auto_now_add=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELD = ['email', 'name']

    objects = UserModelManager()

    def __str__(self):
        return self.email

    def get_short_name(self):
        return self.name[:2].upper()

    def get_full_name(self):
        return self.name

forms.py, where I created the signup form:

class SignUpForm(UserCreationForm):
    email = forms.CharField(required=True, help_text='البريد الإلكترونى الخاص بك - يجب ان يكون حقيقى (يستخدم لتسجيل الدخول) ')
    name = forms.CharField(required=True, help_text='إسمك الحقيقى -  سيظهر كأسم البائع')
    password1 = forms.CharField(widget=forms.PasswordInput,
                                help_text='كلمة المرور - حاول ان تكون سهلة التذكر بالنسبة لك')
    password2 = forms.CharField(widget=forms.PasswordInput,
                                help_text='تأكيد كلمة المرور - إكتب نفس كلمة المرور السابقة مرة أخرى')

    class Meta:
        model = UserModel
        fields = ('email','name',  'password1', 'password2', )
        labels = {
            'name': 'إسمك الحقيقى -  سيظهر كأسم البائع',
            'email': 'البربد الإلكترونى Email',
            'password1': 'كلمة المرور',
            'password2': 'تأكيد كلمة المرور'
        }

views.py with a view that renders the signup template:

def signup(request):
    if request.method == 'POST':
        signup_form = SignUpForm(request.POST)
        if signup_form.is_valid():
            signup_form.save()
            username = signup_form.cleaned_data.get('username')
            raw_password = signup_form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('signup_confirm')
    else:
        signup_form = SignUpForm()

    context = {
        'signup_form': signup_form,
    }
    return render(request, 'fostania_web_app/signup.html', context)

Finally, signup.html, the template itself where the form is being used:

{% extends 'fostania_web_app/base.html' %}
{% block content %}
{% load static %}
    <br>
    <br>
    <div align="center">
    <div class="card border-primary mb-3" style="max-width: 40rem;" align="center">
  <div class="card-header">إنشاء حساب جديد</div>
  <div class="card-body text-dark">
    <h5 class="card-title">
        <img src="{% static 'img/add_user_big.png' %}">
    </h5>
    <p class="card-text">                    {% if user.is_authenticated %}
                        <div align="center">
لقم قمت بتسجيل الدخول بالفعل <br>
                        يمكنك <a href = '{% url 'logout' %}'>

                        <button class="btn btn-danger" >تسجيل الخروج</button> </a> إذا كنت ترغب الدخول على حساب اخر
    </div>
                    {% else %}
          <form method="post" align="right">
    {% csrf_token %}
    {% for field in signup_form %}
      <p>
        {{ field.label_tag }}<br>
        {{ field }}<br>
          <font color="gray">{{ field.help_text }}</font>
        {% for error in field.errors %}
          <p style="color: red">{{ error }}</p>
        {% endfor %}
    {% endfor %}
          <br>
    <button type="submit" class="btn btn-success">تسجيــل</button><br>
  بالضغط على تسجيل انت توافق على شروط الإستخدام الخاصة بموقعنا !
  </form>
                        </div>
    {% endif %}
        </p>
  </div>
</div>

{% endblock %}

The error I'm getting is:

ImproperlyConfigured at /signup/
TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'
like image 398
Ahmed Wagdi Avatar asked Jun 03 '18 20:06

Ahmed Wagdi


1 Answers

I suspect that the problem is actually in your urls.py. If you point your url to the wrong view (maybe a CBV that you have associated to the /signup/ path), that could cause this error.

like image 85
Eric Taurone Avatar answered Sep 28 '22 00:09

Eric Taurone