Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14069

Django site working for all pages except for a few

$
0
0

I'm using a combo of python and django in order to load a site. A HTML home file is also present in the templates folder.

I coded all parts of my site the way I normally do, which has worked for every single instance except for this one. The only app in my site is called "news". Here's the error I recieved after in my terminal after clicking the link python manage.py runserver gave me.

Internal Server Error: /Traceback (most recent call last):  File "C:\Users\Mahsa\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner        response = get_response(request)               ^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\Mahsa\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response    response = wrapped_callback(request, *callback_args, **callback_kwargs)               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "K:\Desktop 2.0\Python 6\irna_project\news\views.py", line 6, in home    return render(request, "news/home.html", {"news", all_news})           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\Mahsa\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\shortcuts.py", line 24, in render    content = loader.render_to_string(template_name, context, request, using=using)              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\Mahsa\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\template\loader.py", line 62, in render_to_string    return template.render(context, request)           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  File "C:\Users\Mahsa\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\template\backends\django.py", line 57, in render    context = make_context(              ^^^^^^^^^^^^^  File "C:\Users\Mahsa\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\template\context.py", line 286, in make_context    raise TypeError(TypeError: context must be a dict rather than set.[28/Mar/2024 02:45:00] "GET / HTTP/1.1" 500 78799Not Found: /favicon.ico[28/Mar/2024 02:45:00] "GET /favicon.ico HTTP/1.1" 404 2334

For more info, here are the main files:

settings.py:

"""Django settings for irna project.Generated by 'django-admin startproject' using Django 5.0.1.For more information on this file, seehttps://docs.djangoproject.com/en/5.0/topics/settings/For the full list of settings and their values, seehttps://docs.djangoproject.com/en/5.0/ref/settings/"""from pathlib import Pathimport os# Build paths inside the project like this: BASE_DIR / 'subdir'.BASE_DIR = Path(__file__).resolve().parent.parent# Quick-start development settings - unsuitable for production# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret!SECRET_KEY = 'django-insecure-5%l9vq54%pm!%%0l)p^r91w6i+^pe5+-3%5v%&2f)eo-6psmcw'# SECURITY WARNING: don't run with debug turned on in production!DEBUG = TrueALLOWED_HOSTS = []# Application definitionINSTALLED_APPS = ['news','django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',]MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',]ROOT_URLCONF = 'irna.urls'TEMPLATES = [    {'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [],'APP_DIRS': True,'OPTIONS': {'context_processors': ['django.template.context_processors.debug','django.template.context_processors.request','django.contrib.auth.context_processors.auth','django.contrib.messages.context_processors.messages',            ],        },    },]WSGI_APPLICATION = 'irna.wsgi.application'# Database# https://docs.djangoproject.com/en/5.0/ref/settings/#databasesDATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': BASE_DIR / 'db.sqlite3',    }}# Password validation# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validatorsAUTH_PASSWORD_VALIDATORS = [    {'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',    },    {'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',    },    {'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',    },    {'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',    },]# Internationalization# https://docs.djangoproject.com/en/5.0/topics/i18n/LANGUAGE_CODE = 'en-us'TIME_ZONE = 'UTC'USE_I18N = TrueUSE_TZ = True# Static files (CSS, JavaScript, Images)# https://docs.djangoproject.com/en/5.0/howto/static-files/STATIC_URL = 'static/'MEDIA_ROOT = os.path.join(BASE_DIR, 'media')MEDIA_URL = 'media/'# Default primary key field type# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-fieldDEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

urls.py:

"""URL configuration for irna project.The `urlpatterns` list routes URLs to views. For more information please see:    https://docs.djangoproject.com/en/5.0/topics/http/urls/Examples:Function views    1. Add an import:  from my_app import views    2. Add a URL to urlpatterns:  path('', views.home, name='home')Class-based views    1. Add an import:  from other_app.views import Home    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')Including another URLconf    1. Import the include() function: from django.urls import include, path    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))"""from django.contrib import adminfrom django.urls import pathfrom news import viewsfrom django.conf import settingsfrom django.conf.urls.static import staticurlpatterns = [    path('admin/', admin.site.urls),    path('', views.home),] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)

home.html:

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>IRNA</title></head><body><h1>Welcome to IRNA</h1>   {% for item in news %}<a href=""><h2>{{ item.title }}</h2></a><h4>{{ item.date }}</h4><img src="{{ item.image.url }}"><p>{{ item.content }}</p><p>-----------------------------</p>   {% endfor %}</body></html>

views.py:

from django.shortcuts import renderfrom .models import Newsdef home(request):    all_news = News.objects.all()    return render(request, "news/home.html", {"news", all_news})# Create your views here.

I'll be happy to provide any other files required in order to solve this problem.


Viewing all articles
Browse latest Browse all 14069

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>