Why I couldn’t use get_user_model() at import time - django

Just as the title, I cannot import usercreationform
. The exception is model haven’t been loaded. It’s only appears in Django 1.10. There’s no such problem in Django 1.11 . I know it depends on whether get_user_model() can be called at runtime. What should I do to solve it in Django 1.10?

You have to use
from django.contrib.auth import(
login,
logout,
get_user_model,
authenticate,
)
at the beginning of the file. Now I hope you can solve your problem :)

Related

Can't import ObtainAuthToken

I am new to Django.
In the course that i'm using he is importing ObtainAuthToken like this:
from rest_framework.authtoken.views import ObtainAuthToken
But when i try to do the same thing i get this exception:
Abstract base class containing model fields not permitted for proxy model 'TokenProxy'.
What am i doing wrong?
I have added both my app and 'rest_framework' to my Installed_Apps.
If this needs clarification i can send my views and urls files also.
Thanks :)
This seems to be a bug in Django REST Framework 3.12.x. https://github.com/encode/django-rest-framework/issues/7561
For the time being, downgrade to 3.11.x (pip install -U 'djangorestframework<3.12').

Debugging while developing Django apps

I'm aware of pdb built-in Python library for debugging Python programs and scripts. However, you can't really use it for debugging Django apps (I get errors when I try to use it in views.py). Are there any tools that I can use when Django's traceback isn't helpful ?
EDIT:
from .forms import TestCaseForm, TestCaseSuiteForm
from .models import TestCase, TestSuite
from django.contrib.auth.forms import UserCreationForm
from django.views.generic import FormView, ListView
from django.contrib.auth.models import User
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
import pdb
class ListAllTestSuites(ListView):
template_name = 'list.html'
context_object_name = 'username'
def get_queryset(self):
pdb.set_trace() # <-- setting a trace here to diagnose the code below
queryset = {'test_suites': TestSuite.objects.filter(user=self.request.user),
'username': self.request.user}
return queryset
you forgot the exact error message and full traceback, and, more importantly, you forgot to explain how you executed this code to get this result...
But anyway: from the error message, you're obviously trying to execute your view file as a plain python script (cf the reference to __main__). This cannot work. A view is a module, not a script, and, moreover, any module dependending on Django needs some setup done to be imported (which is why we use the django shell - ./manage.py shell - instead of the regular Python one).
For most case, you can just launch the django shell, import your module and use pdb.runcall() to execute some function / method under the debugger (no need to put a breakpoint then, but that's still possible).
Now views require a HTTPRequest as first argument which make them a bit more cumbersome to debug that way (well, there is django.tests.RequestFactory but still...), so your best bet here is to set your breakpoint, launch your devserver (or restart it - if it didn't already did, as it should), point your browser to the view's url, and then you should see the debugger's prompt in your devserver's terminal.

Django unable to load model into views

I am trying to import my models into views.py but I am unable to do so. However I am able to register them on the admin site but when I use the same code I used in admin.py to import the models into views.py, I get an error. I am using djongo so I am not sure if that changes anything about how to import them and I cannot seem to find the documentation for it.
models.py
from djongo import models
class Round(models.Model):
round_num = models.IntegerField(default=0)
admin.py
from django.contrib import admin
from .models import Round
admin.site.register(Round)
views.py
from .models import Round
When I try and run my views.py file I get the following error: ModuleNotFoundError: No module named 'main.models'; 'main' is not a package
Also my views, admin, and models file are all in the same directory. I have made the migrations and I can see my Round model in MongoDB. The only thing I cannot do is import it to the view
You need to have an __init__.py file in your directory. It should be inside of your main folder and at the same level as your views.py and models.py
As a workaround, since the models properly migrate to MongoDB. Using pymongo I have just connected to Mongo and have rendered data into my views this way. It works fine so if anybody else has an issue loading in their models, you can always just connect directly to the DB.

django ckeditor image upload

I'm using Django-ckeditor in my website.
I'm especially using
RichTextUploadingField()
in my model. and other option just works fine, except image upload.
1. Error Message
I'm getting an error message of
"Incorrect Server Response" and especially, chrome devtools indicates that
ckeditor.js:21 [CKEDITOR] Error code: filetools-response-error.
ckeditor.js:21 [CKEDITOR] For more information about this error go to https://docs.ckeditor.com/ckeditor4/docs/#!/guide/dev_errors-section-filetools-response-error
2. Guess
I have tried uploading images using ckeditor in my admin page,
authorized as superuser in django, it works.
However, logged in as the normal user account, I've tried the same thing, but it does not work.
So my guess is it has some kind of authorization problem. But I can't figure out where to start debugging in my django-ckeditor.
What things should I be checking? Thanks in advance.
This is happening because the default urls are decorated with #staff_member_required(https://github.com/django-ckeditor/django-ckeditor/blob/master/ckeditor_uploader/urls.py). To avoid this, instead of including the urls like so url(r'^ckeditor/', include('ckeditor_uploader.urls')) you could define them one by one in your urls.py with the login_required decorator:
from django.conf.urls import url
from django.contrib.auth.decorators import login_required
from ckeditor_uploader import views
urlpatterns = [
.....your other urls
url(r'^ckeditor/upload/', login_required(views.upload), name='ckeditor_upload'),
url(r'^ckeditor/browse/', never_cache(login_required(views.browse)), name='ckeditor_browse'),
]
Like this you are limiting the uploads to all users that are logged in.
Add following imports in the project urls.py:
from django.contrib.auth.decorators import login_required
from django.views.decorators.cache import never_cache
from ckeditor_uploader import views as ckeditor_views
Replace the following row in the urls.py:
path('ckeditor/', include('ckeditor_uploader.urls')),
with
path('ckeditor/upload/', login_required(ckeditor_views.upload), name='ckeditor_upload'),
path('ckeditor/browse/', never_cache(login_required(ckeditor_views.browse)), name='ckeditor_browse'),
it works if you are logged in as an admin(localhost:8000/admin), simple is that.

Django 2.0 getting all model list

I need a list of django models registered, Just like django admin index page. I'm using django 2.0 where from django.contrib.admin.validation import validate and from django.db.models import get_models is outdated. Advance thanks.
from django.apps import apps
apps.get_models()
https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.get_models
For a particular application:
from django.apps import apps
apps.all_models['<app_name>']
https://stackoverflow.com/a/1126209/196834
from django.contrib.admin.validation import validate
It was deprecated.
https://docs.djangoproject.com/en/2.0/releases/1.7/#modeladmin-validators