Home > Software engineering >  django throws an error when there is no row in the table! django.db.utils.OperationalError: no such
django throws an error when there is no row in the table! django.db.utils.OperationalError: no such

Time:03-07

mainly the issue is creating on views.py queryset! I've tried with an existing database with some rows...there was nothng wrong! but if I remove all rows from admin panel then it shows this error django.db.utils.OperationalError: no such table: home_general

these queries make error if table is empty

general = General.objects.first()
social = Social_URI.objects.all()
seo = SEO.objects.first()
project_data = Project.objects.all()

if I bind these inside try-except then there is no erros happen like django.db.utils.OperationalError: no such table: but again I face error on template because of empty row!

from django.shortcuts import render, redirect
from django.views.decorators.http import require_http_methods
from .models import Event, Comment
from home.models import General, Social_URI, SEO
from projects.models import Project

try:
    general = General.objects.first()
except:
    general = None
try:
    social = Social_URI.objects.all()
except:
    social = None
try:
    seo = SEO.objects.first()
except:
    seo = None
try:
    event_data = Event.objects.all()
except:
    event_data = None
try:
    project_data = Project.objects.all()
except:
    project_data = None

def event(request):
    try:
        data = {
            "logo": general.logo,
            "favicon": general.favicon,
            "title": general.title,
            "top_phone": general.top_phone,
            "top_email": general.top_email,
            "about_image": general.about_image,
            "about_title": general.about_title,
            "about_text": general.about_text,
            "footer_logo": general.footer_logo,
            "footer_text": general.footer_text,
            "footer_copyright": general.footer_copyright,
            "footer_address": general.footer_address,
            "social_url": social,
            "ga_id": seo.ga_id,
            "seo_author": seo.author,
            "seo_description": seo.description,
            "seo_keywords": seo.keywords,
            "events_data": event_data,
            "projects_data": project_data
        }
    except:
        data = None
    return render(request, "events/event.html", data)


def event_view(request, id):
    event_view_data = Event.objects.get(pk=id)
    comments = Comment.objects.filter(event=id)
    data = {
        "logo": general.logo,
        "favicon": general.favicon,
        "title": general.title,
        "top_phone": general.top_phone,
        "top_email": general.top_email,
        "about_image": general.about_image,
        "about_title": general.about_title,
        "about_text": general.about_text,
        "footer_logo": general.footer_logo,
        "footer_text": general.footer_text,
        "footer_copyright": general.footer_copyright,
        "footer_address": general.footer_address,
        "social_url": social,
        "ga_id": seo.ga_id,
        "seo_author": seo.author,
        "seo_description": seo.description,
        "seo_keywords": seo.keywords,
        "event_id": event_view_data.id,
        "event_title": event_view_data.title,
        "event_thumb": event_view_data.thumb.url,
        "event_desc": event_view_data.description,
        "event_amount": event_view_data.amount,
        "event_location": event_view_data.location,
        "event_calender": event_view_data.calender,
        "comments": comments,
        "projects_data": project_data
    }
    return render(request, "events/event_view.html", data)

@require_http_methods(["POST"])
def comment(request):
    event_id = request.POST.get("event_id")
    username = request.POST.get("username")
    email = request.POST.get("email")
    message = request.POST.get("message")
    event = Event.objects.get(pk=event_id)
    comment = Comment(event=event, username=username, email=email, message=message)
    comment.save()
    return redirect("event_view", id=event_id)
Exception Value:    
Reverse for 'event_view' with arguments '('',)' not found. 1 pattern(s) tried: ['event_view/(?P<id>[0-9] )\\Z']

CodePudding user response:

Good day!

Usually, the django.db.utils.OperationalError: no such table happens when you create models in models.py, but don't run migrate to the freshly created database, more here: the Django: OperationalError No Such Table

Where as for Reverse for 'event_view' with arguments '('',)' not found. 1 pattern(s) tried: ['event_view/(?P<id>[0-9] )\\Z']: it says that you are trying to open the url event_view/ without id. It has to be something like event_view/1/, more is here: Reverse for 'view_item' with arguments '('',)' and keyword arguments '{}' not found

  • Related