Exception in thred django-main-thread: [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Creating forms from models
i'm new to django and try to models form but unfortunate get some bizarre error even i twice check my code as well i checked django documentation but not figure out my issue...
i visit this django documentation but not figure out yet!!! quite confusing!
https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/
AppFive/forms.py
from django import forms
from AppFive.models import User
from django.forms import ModelForm
class NewUserForm(ModelForm):
class Meta:
model = User
fields = '__all__ ' # <-- Mistake over here - This Line. SOLVED!
AppFive/models.py
from django.db import models
# Create your models here.
class User(models.Model):
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
email = models.EmailField(max_length=264,unique=True)
AppFive/views.py
from django.shortcuts import render
# from django.http import HttpResponse
# from AppFive.models import User
# Create your views hereself.
from AppFive.forms import NewUserForm
def index(request):
return render(request,'AppFive/index.html')
def users(request):
form = NewUserForm()
if request.method == "POST":
form = NewUserForm(request.POST)
if form.is_valid():
form.save(commit=True)
return index(request)
else:
print('ERROR : F O R M I N V A L I D')
return render(request,'AppFive/users.html',{'form':form})
Traceback (most recent call last):
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\management\base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\urls\resolvers.py", line 579, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\utils\functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\urls\resolvers.py", line 572, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Khan\Desktop\Python Bootcamp\PB2\ProFive\ProFive\urls.py", line 18, in <module>
from AppFive import views
File "C:\Users\Khan\Desktop\Python Bootcamp\PB2\ProFive\AppFive\views.py", line 5, in <module>
from AppFive.forms import NewUserForm
File "C:\Users\Khan\Desktop\Python Bootcamp\PB2\ProFive\AppFive\forms.py", line 5, in <module>
class NewUserForm(ModelForm):
File "C:\Users\Khan\Miniconda3\envs\MyDjangoEnv\lib\site-packages\django\forms\models.py", line 235, in __new__
raise TypeError(msg)
TypeError: NewUserForm.Meta.fields cannot be a string. Did you mean to type: ('__all__ ',)?
python 3.7.4
django (2, 2, 3, 'final', 0)

fields = '__all__ '
It has an extra space in the string. Remove it!

As mentioned by caot, you need to provide the code for correct answer. Anyway, let me make a guess.
When you try to generate a form out of a model, you need to specify the model as well as the fields in it (that you are interested in displaying / capturing).
The fields should be assigned a list consisting (strictly) of field names, each as a string. If you intend to include all fields, 'all' as a string (yes, string; not a list) can be specified.
I guess, either you made a mistake in specifying all or you included the fields as a string (fields = 'pub_date' or fields = 'pub_date, headline')
If my answer does not solve your issue, please show the full code.
Cheers

Related

(update)I want to use runserver to make some objects with admin but it says the object has no attribute 'urls'

I have registered the models in admin.py
the whole things I've changed in this app are in admin.py and models.py
I didn't created the urls.py
When I commented the registration in admin.py i was able to migrate the models.py changes but now when i registering the models with admin i can't use runserver and to create objects i should use the shell command
i just wanted to add objects from admin not to use the shell and i think if i don't debug this will be a problem sometime later again
this is my models.py:
from django.db import models
# Create your models here.
class Clients(models.Model):
name = models.CharField(max_length=300)
company = models.CharField(max_length=300)
def __str__(self) -> str:
return self.company
class Manufacturers(models.Model):
name = models.CharField(max_length=300)
location = models.TextField("address")
def __str__(self) -> str:
return self.name
class Products(models.Model):
cost_per_item = models.PositiveBigIntegerField("Cost")
name_of_product = models.CharField("name", max_length=300)
manufacturer = models.ForeignKey(Manufacturers, on_delete=models.CASCADE)
def __str__(self) -> str:
return self.name
class ClientOrders(models.Model):
fulfill_date = models.PositiveIntegerField("Fulfill Month")
order_number = models.PositiveIntegerField(primary_key=True)
client = models.ForeignKey(Clients, on_delete=models.CASCADE)
products = models.ManyToManyField(Products)
def __str__(self) -> str:
return self.client
this is admin.py:
from django.contrib import admin
from .models import Clients, ClientOrders, Manufacturers, Products
# Register your models here.
admin.site.register(Clients, ClientOrders)
admin.site.register( Manufacturers, Products)
this is the traceback:
(Django_learn) D:\learnD\learnF1>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\threading.py", line 950, in _bootstrap_inner
self.run()
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\threading.py", line 888, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 419, in check
all_issues = checks.run_checks(
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py", line 412, in check
for pattern in self.url_patterns:
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "D:\learnD\learnF1\config\urls.py", line 20, in <module>
path('admin/', admin.site.urls),
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\functional.py", line 247, in inner
return func(self._wrapped, *args)
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\sites.py", line 299, in urls
return self.get_urls(), 'admin', self.name
File "C:\Users\Armin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\sites.py", line 279, in get_urls
path('%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls)),
AttributeError: 'ClientOrders' object has no attribute 'urls'

How can I resolve an" ImportError: attempted relative import with no known parent package" issue in a Django project?

I'm working on Chapter 18 of Python Crash Course, and have started over from scratch twice already, all 53 steps. (A very educational process)
The program was working fine until I tried to add the code for individual pages. Now, it won't run at all.
It keeps saying that views aren't defined. I've tried every suggestion I've found for similar issues without success and double checked all the elements.
Here is the code for my urls.py and views.py.
Any information/suggestions on how to resolve this would be greatly appreciated.
"""Defines URL patterns for learning_logs"""
from django.urls import path
from . import views
app_name = 'learning_logs'
urlpatterns = [
path('topics/<int:topic_id>>/',views.topic,name = 'topic'),
path('', views.index,name='index'),
path('topics/',views.topics, name='topics'),
]
from .models import Topic
from django.shortcuts import render
def topics(request):
topics = Topic.objects.order_by('date_added')
context = {'topics':topics}
return render(request,'learning_logs/topics.html',context)
def index(request):
return render(request,'learning_logs/index.html')
def topic(request, topic_id):
topic = Topic.objects.get(id= topic_id)
entries = topic.entry_set.order_by('-date_added')
context = {'topic': topic,'entries':entries}
return render(request, 'learning_logs/topic.html',context)
Here is the long error message I receive in the terminal:
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 954, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/core/management/base.py", line 392, in check
all_issues = checks.run_checks(
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/core/checks/registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/urls/resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/urls/resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/andreawalker/Desktop/learning_log/ll_env/lib/python3.9/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/andreawalker/Desktop/learning_log/learning_log/urls.py", line 23, in <module>
path('topics/', views.topics, name='topics'),
NameError: name 'views' is not defined
Here's the last few lines of the traceback:
File "/Users/andreawalker/Desktop/learning_log/learning_log/urls.py", line 23, in <module>
path('topics/', views.topics, name='topics'),
NameError: name 'views' is not defined
This is a pretty common error; it usually means you forgot an import statement. You've combined your urls.py and models.py files in this post, but it looks like you have from . import views at the top of your urls.py file. It's also odd that it would complain about views being undefined in the topics/ path, but not in the two paths defined before that.
You said that you've started the project over several times. My best guess is that the urls.py file from a previous attempt is still being used. I can think of a number of ways this might happen. Here's the path to the urls.py file that's being used:
File "/Users/andreawalker/Desktop/learning_log/learning_log/urls.py"
Is this the urls.py file from your current attempt? If not, close out all open terminals, editors, etc. Then open a terminal and navigate to the folder where your current attempt lives. Activate the virtual environment in that directory, run your project, and see if you get the same error.

Django form include Many2Many field from related_name

I have two models with two update forms.
Now I want to achieve two things:
be able to edit a certificate and set all the servers this certificate is used on
be able to update a server and set all the certificates which are used on this server.
class Certificate(models.Model):
internal_name = models.CharField(max_length=1024)
pem_representation = models.TextField(unique=True)
servers = models.ManyToManyField(
Server, related_name='certificates', blank=True)
class Server(models.Model):
name = models.CharField(max_length=1024, unique=True)
class CertificateUpdateForm(forms.ModelForm):
class Meta:
model = models.Certificate
fields = ['internal_name', 'pem_representation', 'servers']
class ServerUpdateForm(forms.ModelForm):
class Meta:
model = models.Server
fields = ['name', 'certificates']
Without the field "certificates" in ServerUpdateForm I get no error but when updating via the form the changes for the certificates just aren't recognized.
The error message I get with this code is:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib64/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib64/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File " venv/lib64/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File " venv/lib64/python3.8/site-packages/django/core/management/commands/runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File " venv/lib64/python3.8/site-packages/django/core/management/base.py", line 392, in check
all_issues = checks.run_checks(
File " venv/lib64/python3.8/site-packages/django/core/checks/registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File " venv/lib64/python3.8/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File " venv/lib64/python3.8/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File " venv/lib64/python3.8/site-packages/django/urls/resolvers.py", line 408, in check
for pattern in self.url_patterns:
File " venv/lib64/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File " venv/lib64/python3.8/site-packages/django/urls/resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File " venv/lib64/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File " venv/lib64/python3.8/site-packages/django/urls/resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib64/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File " certmanager/certmanager/urls.py", line 26, in <module>
path('servers/', include('servers.urls')),
File " venv/lib64/python3.8/site-packages/django/urls/conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "/usr/lib64/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File " certmanager/servers/urls.py", line 3, in <module>
from . import views
File " certmanager/servers/views.py", line 9, in <module>
from . import forms, models
File " certmanager/servers/forms.py", line 12, in <module>
class ServerUpdateForm(forms.ModelForm):
File " venv/lib64/python3.8/site-packages/django/forms/models.py", line 268, in __new__
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (certificates) specified for Server
How can I update the M2M relation on both objects?
So I asked the same question in a post on the Django forum and got a working suggestion.
For better viewability:
I overrode the post-method of the UpdateView I use and managed the Many2Many relationship there. I also had to remove the 'certificates' field form the UpdateForm obviously because it thew an exception.
Here is the full UpdateView class I use:
class ServerUpdate(PermissionRequiredMixin, generic.UpdateView):
model = models.Server
template_name = 'servers/update.html'
form_class = forms.ServerUpdateForm
queryset = models.Server.objects.all()
success_url = reverse_lazy('servers:server-index')
permission_required = ('servers.change_server')
def get_context_data(self, **kwargs):
context = super(ServerUpdate, self).get_context_data(**kwargs)
context['all_certificates'] = Certificate.objects.all()
context['selected_certificates'] = self.object.certificates.all()
return context
def post(self, request, *args, **kwargs):
selected_certificates = request.POST.getlist('certificates')
self.object = self.get_object()
self.object.certificates.set(selected_certificates)
return super().post(request, *args, **kwargs)

TypeError: Cannot create a consistent method resolution in django

I am learning django through the 'Django 3 by example' book, and right now i am trying to build an e - learning platform. But, I am getting a weird error in my views.py file.
Here is my views file:
from django.views.generic.list import ListView
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from .models import Course
class ManageCourseListView(ListView):
model = Course
template_name = 'educa/manage/educa/list.html'
permission_required = 'educa.view_course'
def get_queryset(self):
qs = super().get_queryset()
return qs.filter(owner=self.request.user)
class OwnerMixin(object):
def get_queryset(self):
qs = super().get_queryset()
return qs.filter(owner=self.request.owner)
class OwnerEditMixin(object):
def form_valid(self, form):
form.instance.onwer = self.request.user
return super().form_valid(form)
class OwnerCourseMixin(object, LoginRequiredMixin, PermissionRequiredMixin):
model = Course
fields = ['subject', 'title', 'slug', 'overview']
success_url = reverse_lazy('manage_course_list')
class OwnerCourseEditMixin(OwnerCourseMixin, OwnerEditMixin):
template_name = 'educa/manage/course/list.html'
class CourseCreateView(OwnerEditMixin, CreateView):
permission_required = 'educa.add_course'
class CourseUpdateView(OwnerCourseEditMixin, UpdateView):
permission_required = 'educa.change_course'
class CourseDeleteView(OwnerCourseMixin, DeleteView):
template_name = 'educa/manage/course/delete.html'
permission_required = 'educa.delete_course'
The error is on line 30.
Here is the full error message:
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Padma Jain\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Padma Jain\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run
self.check(display_num_errors=True)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\core\management\base.py", line 392, in check
all_issues = checks.run_checks(
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\urls\resolvers.py", line 408, in check
for pattern in self.url_patterns:
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\urls\resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\urls\resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\Padma Jain\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Padma Jain\Desktop\django\educa\elearn\elearn\urls.py", line 24, in <module>
path('course/',include('educa.urls')),
File "C:\Users\Padma Jain\Desktop\django\educa\venv\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\Padma Jain\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Padma Jain\Desktop\django\educa\elearn\educa\urls.py", line 2, in <module>
from .views import *
File "C:\Users\Padma Jain\Desktop\django\educa\elearn\educa\views.py", line 30, in <module>
class OwnerCourseMixin(object, LoginRequiredMixin, PermissionRequiredMixin):
TypeError: Cannot create a consistent method resolution
order (MRO) for bases object, LoginRequiredMixin, PermissionRequiredMixin
And just if it is required, here is the urls.py file of the educa application:
from django.urls import path
from .views import *
urlpatterns = [
path('mine/',ManageCourseListView.as_view(),name='manage_course_list'),
path('create/',CourseCreateView.as_view(),name='course_create'),
path('<pk>/edit/',CourseUpdateView.as_view(),name='course_edit'),
path('<pk>/delete/',CourseDeleteView.as_view(),name='course_delete'),
]
This is my first time building such a large application. Can someone please tell where I'm wrong?
EDIT:
I solved this problem by editing line 30 of my views.py file from this:
class OwnerCourseMixin(object, LoginRequiredMixin, PermissionRequiredMixin):
....
to this:
class OwnerCourseMixin(OwnerMixin, LoginRequiredMixin, PermissionRequiredMixin):
....
In Python, every class inherits from a built-in basic class called object.
Your OwnerCourseMixin is inheriting from object and some other class. Because the other class(es), in this case, LoginRequiredMixin and PermissionRequiredMixin, already inherit from object, Python now cannot determine what class to look methods up on first.
You don't need to inherit from object here.
class OwnerCourseMixin(LoginRequiredMixin, PermissionRequiredMixin):
model = Course
fields = ['subject', 'title', 'slug', 'overview']
success_url = reverse_lazy('manage_course_list')
That should work.

Error while creating a view and displays some errors which goes like explicit label and isn't inside the INSTALLED APPS

the Entire error message is:
Exception ignored in thread started by: <function check_errors.<locals>.wrapper at 0x000001967F417820>
Traceback (most recent call last):
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\utils\autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\core\management\base.py", line 356, in check
all_issues = self._run_checks(
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
return check_method()
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\urls\resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\urls\resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\urls\resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "c:\python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\ALEX GEORGE\Dev\cfehome\src\trydjango\urls.py", line 19, in <module>
from src.products.views import product_detail_view
File "C:\Users\ALEX GEORGE\Dev\cfehome\src\products\views.py", line 2, in <module>
from .models import Product
File "C:\Users\ALEX GEORGE\Dev\cfehome\src\products\models.py", line 5, in <module>
class Product(models.Model):
File "C:\Users\ALEX GEORGE\Dev\cfehome\lib\site-packages\django\db\models\base.py", line 115, in __new__
raise RuntimeError(
RuntimeError: Model class src.products.models.Product doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
I have used the solution which was mentioned earlier like using 'django.contrib.sites' and setting SITE ID =1, But Im still getting the same error. My setting goes like this
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
# own
'pages',
'products'
]
SITE_ID = 1;
I got this error while i created a product directory inside a template directory and I named the template as product.html. My apps name is products. enter code here
Here is my model
from django.db import models
# Create your models here.
class Product(models.Model):
title = models.CharField(max_length=120) # max_length is required
description = models.TextField(blank=True, null=True)
price = models.DecimalField(decimal_places=2, max_digits=10000)
summary = models.TextField(blank=False, null=False)
featured = models.BooleanField() # null=True, default= True
here is my view inside products
from django.shortcuts import render
from .models import Product
# Create your views here.
def product_detail_view(request):
obj = Product.objects.get(id=1)
context = {
'title': obj.title,
'description': obj.description
}
return render(request, "product/detail.html", context)
here is my pages apps view
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def home_view(request, *args, **kwargs):
print(args, kwargs)
print(request.user)
# return HttpResponse("<h1>Hello World</h1>") # html string code
return render(request, "home.html")
def contact_view(request, *args, **kwargs):
return render(request, "contact.html")
def about_view(request, *args, **kwargs):
my_context = {
"my_text": "This is about us",
"my_number": 123,
"my_list": [123, 3223, 1323]
}
return render(request, "about.html", my_context)
In your products/views.py you should write
from products.models import Product
instead of
from .models import Product
And restart the server
And if you still getting error try this
AppConfig

Categories