P
P
P4YK2021-05-11 23:06:30
Django
P4YK, 2021-05-11 23:06:30

Can't make a user profile?

Hello, please help! I can't make a user profile.
Error: Migrations609ae277464cc833851597.png

folder (0001_initial.py) :

from django.conf import settings                                                       
from django.db import migrations, models                                               
import django.db.models.deletion                                                       
                                                                                       
                                                                                       
class Migration(migrations.Migration):                                                 
                                                                                       
    initial = True                                                                     
                                                                                       
    dependencies = [                                                                   
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),                     
    ]                                                                                  
                                                                                       
    operations = [                                                                     
        migrations.CreateModel(                                                        
            name='Profile',                                                            
            fields=[                                                                   
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize$
                ('image', models.ImageField(default='default.jpg', upload_to='profile_$
                ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASC$
            ],                                                                         
        ),                                                                             
    ]


models.py :
from django.db import models                                                           
from django.contrib.auth.models import User                                            
                                                                                       
                                                                                       
class Profile(models.Model):                                                           
    user = models.OneToOneField(User, on_delete=models.CASCADE)                        
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')         
                                                                                       
    def __str__(self):                                                                 
        return f'{self.user.username} Profile'


views.py :
from django.shortcuts import render, redirect                                          
from django.contrib import messages                                                    
from .forms import UserOurRegistration                                                 
from django.contrib.auth.decorators import login_required                              
                                                                                       
def register(request):                                                                 
    if request.method == "POST":                                                       
        form = UserOurRegistration(request.POST);                                      
        if form.is_valid():                                                            
            form.save()                                                                
            username = form.cleaned_data.get('username')                               
            return redirect('home')                                                    
    else:                                                                              
        form = UserOurRegistration()                                                   
    return render(request, 'users/registration.html', {'form': form})                  
                                                                                       
@login_required                                                                        
def profile(request):                                                                  
    return render(request, 'users/profile.html')


login.html :
% extends 'web/main.html' %}                                                          
{% load crispy_forms_tags %}                                                           
{% block title_page %}                                                                 
  <title>Авторизация на сайте</title>                                                  
{% endblock title_page %}                                                              
                                                                                       
{% block content %}                                                                    
<div class="container" style="margin-top: 5%;">                                        
    <div class="form-section">                                                         
        <h2>Авторизация</h2>                                                           
        <form method="POST">                                                           
            {% csrf_token %}                                                           
            {{ form|crispy }}                                                          
            <button type="submit" class="btn btn-ouline-warning">Войти</button>        
        </form>                                                                        
        <hr>                                                                           
        Нету профиля? <a href="{% url 'reg' %}">Зарегистрироваться</a>                 
    </div>                                                                             
</div>                                                                                 
{% endblock content %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2021-05-12
@P4YK

A user profile must also be created. Ideally, at the User post_save signal, to cover all possible places of creation at once.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question