0

I'm building my first partly "independent" project without utilizing a tutorial. I made an Event object which the user could assign the title, date_due, and description. I am currently attempting to add an author value to it, which would hold the user's profile. I'm using the django.contrib.auth.models User model.

I'm not entirely sure of what I am doing, I would just like some help on my project overall. I'm rather young in early high school and am feeling really lost currently. Extremely appreciated, thanks.

This is my Blog/models.py, holding my Event model.

from django.db import models
from django.contrib.auth.models import User

# Create your models here.


class Event(models.Model):
    title = models.CharField(max_length=100)
    date_due = models.DateTimeField(auto_now_add=False, auto_now=False, blank=True)
    description = models.TextField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)

This is my Blog/forms.py file, for the Event object.

from django import forms
from .models import Event


class EventCreationForm(forms.ModelForm):

    class Meta:
        model = Event
        fields = ['title', 'date_due', 'description']

This is my Blog/views.py, which displays the Event form as well as a list of all of the User's events.

from django.shortcuts import render
from .models import Event
from .forms import EventCreationForm

# Create your views here.


def CalendarView(request):
    if request.method == "POST":
        form = EventCreationForm(request.POST)
        if form.is_valid():
            form.save()
    else:
        form = EventCreationForm

    context = {
        'events': Event.objects.all(),
        'form': form
    }

    return render(request, 'Blog/calendar.html', context)

Although I'm not sure this is necessary, I'm just making sure I'm getting enough to provide.

This is my User/views.py, my views file in my User app that takes care of the User/Profile things.

from django.shortcuts import render, redirect
from .forms import UserRegisterForm
from django.contrib.auth.decorators import login_required

# Create your views here.


def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)  # saves the form inputs from request.POST
        if form.is_valid():
            form.save()

            return redirect('login')
    else:
        form = UserRegisterForm()

    return render(request, 'User/register.html', {'form': form})


@login_required
def profile(request):

    return render(request, 'User/profile.html')

This is my User/models.py, my really simple User model,

from django.db import models
from django.contrib.auth.models import User

# Create your models here.


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

My User/forms.py file, just my registration form that I built off of the django UserCreationForm,

from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm


class UserRegisterForm(UserCreationForm):

    class Meta:
        model = User  # model that will be affected is the user model
        fields = ['username', 'first_name', 'last_name', 'password1', 'password2']

Please, ask away anything that you need from me to help, I am willing to do anything. I am really passionate about coding, and I have spent too much time trying to figure this out on my own. I need help. First question on stack overflow btw so give me tips.

Error with full traceback:

  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute

    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: Blog_event.author_id

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response

    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response

    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\User\PycharmProjects\FirstProject\MyProject\Blog\views.py", line 29, in CalendarView
    return render(request, 'Blog/calendar.html', context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\template\defaulttags.py", line 166, in render
    len_values = len(values)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\models\query.py", line 258, in __len__
    self._fetch_all()
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\models\query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\models\query.py", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1151, in execute_
sql
    cursor.execute(sql, params)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_w
rappers
    return executor(sql, params, many, context)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\User\PycharmProjects\FirstProject\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute

    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such column: Blog_event.author_id

Again I am new to this so any help would be appreciated.

Blog/0001_initial.py file,

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='Event',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(max_length=100)),
                ('date_due', models.DateTimeField(blank=True)),
                ('description', models.TextField()),
                ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
        ),
    ]
Sno Bear
  • 15
  • 1
  • 6
  • 1
    Please post the error message with full traceback. – Selcuk May 28 '20 at 05:43
  • 1
    Is this the error message with full traceback? – Sno Bear May 28 '20 at 06:24
  • 3
    Yes. You seem to have forgotten to do a migration (`manage.py makemigrations` & `manage.py migrate`) after adding the `author` field to your `Event` model. – Selcuk May 28 '20 at 06:34
  • That is the weird part. I did run those two lines and it still would display the same error. I will post my 0001_initial.py file. Is it bad that I have deleted my initial.py file multiple times to reset my migrations? – Sno Bear May 28 '20 at 14:59
  • @Selcuk I think I may have found the problem. I have had previous Event objects in my database that did not have the author attribute. Could that be the problem? – Sno Bear May 28 '20 at 15:13
  • Never mind, I have found the solution. After many hours of research and Netflix, I finally figured out that since I had previous Event objects within my database, I had to make the author attribute nullable. – Sno Bear May 29 '20 at 03:41
  • Right, this means that you got an error when you run `makemigrations` which you ignored. Deleting a migration file is usually not recommended unless you also delete the corresponding table and the rows in the migrations table. – Selcuk May 29 '20 at 06:48
  • @Selcuk, how do you delete the corresponding table and the rows in the migration table? – Sno Bear May 29 '20 at 15:07
  • See [this question](https://stackoverflow.com/questions/29253399/how-to-reset-migrations-in-django-1-7). – Selcuk May 30 '20 at 05:43

0 Answers0