Django incorrect import - django

I am developing a web app with django 1.2.4, but I am having a problem with the Site model. I try:
from django.contrib.sites.models import Site
...
if Site._meta.installed:
...
I am getting the error undefined variable from import: _meta in the if statement,
any help?

Unless you've fiddled with the django source, there really should be any problems with the Sites._meta.installed variable. _meta.installed is assigned from within the metaclass of all models (using contribute_to_class()) so it would affect ALL models if the code were broken.
A quick search for relevant tickets does not reveal such a problem for that version (or any other version) of django.
Are you by any chances running django via pydev? If so, perhaps this post is relevant: How do I fix PyDev "Undefined variable from import" errors?
That's of course a wild speculation on my part. If you can post a Trackback of your error, we might be able to get a better insight into your problem.
Response to comments:
"I get the error in the IDE (apatana Studio 3)"
Aptana uses PyDev and so will exhibit the same problem. Here's a possible fix taken from this blog post:
Open up Aptana Studio
Open Window > Preferences > PyDev > Editor > Code Analysis
Select the “Undefined” tab
Add DoesNotExist at the end of the “Consider the following names as globals” list
Apply and restart

Related

Django Upgrade from 1.11 to 2.2.1 Problem with URLs and Paths

Today I decided to upgrade my project from Django 1.11 to 2.2.1. I've been working through various issues with my project, and I'm fighting through them. However, I spent most of tonight trying to get the URLs to work and they won't cooperate. Long story short, I have multiple app in my project and each app has a URL with it's own namespace. In Django 1.11 this is working fine. However, when I try to port the logic over to Django 2.2.1, I keep getting an error saying that I have a circular import somewhere probably.
Here is a snippit of what works just fine in Django 1.11.......
My Main Project...In Django 1.11
url(r'^Main/',include('AppA.urls',namespace="AppA")),
But when I try to do this in Django 2.2.1.....
I realize that URLs were replaced by paths...
path('', include('AppA.urls')),
But when I try to start my application it says....
your project does not appear to have any patterns in it. If you see valid p
atterns in the file then the issue is probably caused by a circular import.
I can't seem to figure out how to create the namespace that is working in django 1.11 so that I can properly reference my urls in my templates.
I've been staring at this most of the evening and that might be why I'm not seeing it...I also looked at the Django doc...https://docs.djangoproject.com/en/2.2/topics/http/urls/
And I just can't see what I might be doing wrong. Thanks in advance for any help to get me back on track.
As Bloodmallet pointed out to me...
I needed to add....
app_name = 'Appa'
to the top of my urls.py file. After doing this, the path URL worked as expected.
Instead of path(), consider using re_path():
from django.urls import include, re_path
re_path(r'^Main/',include('AppA.urls',namespace="AppA")),

Django doesn't read from database – no error

I just set up the environment for an existing Django project, on a new Mac. I know for certain there is nothing wrong with the code itself (just cloned the repo), but for some reason, Django can't seem to retrieve data from the database.
I know the correct tables and data is in the db.
I know the codebase is as it should be.
I can make queries using the Django shell.
Django doesn't throw any errors despite the data missing on the web page.
I realize that it's hard to debug this without further information, but I would really appreciate a finger pointing me to the right direction. I can't seem to find any useful logs.
EDIT:
I just realized the problem lies elsewhere. Unfortunately I can't delete this post with the bounty still open.
Without seeing any code, I can only suggest some general advice that might help you debug your problem. Please add a link to your repository if you can or some snippets of your database settings, the view which includes the database queries etc...
Debugging the view
The first thing I would recommend is using the python debugger inside the view which queries the database. If you've not used pdb before, it's a life saver which allows you to set breakpoints in your Python script and then interactively execute code inside the interpreter
>>> import pdb
>>> pdb.set_trace()
>>> # look at the results of your queries
If you are using the Django ORM, the QuerySet returned from the query should have all the data you expect.
If it doesn't then you need to look into your database configuration in settings.py.
If it does, then you must might not be returning that object to the template? Unlikely as you said the code was the same, but double check the objects you pass with your HttpResponse object.
Debugging the database settings
If you can query the database using the project settings inside settings.py from the django shell it sounds unlikley that there is a problem with this - but like everything double check.
You said that you've set up a new project on a mac. What is on a different operating system before? Maybe there is a problem with the paths now - to make your project platform independent remember to use the os.path.join() method when working with file paths.
And what about the username and password details....
Debugging the template
Maybe your template is referencing the wrong object variable name or object attribute.You mentioned that
Django doesn't throw any errors despite the data missing on the web
page.
This doesn't really tell us much - to quote the Django docs -
If you use a variable that doesn’t exist, the template system will
insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is
set to '' (the empty string) by default.
So to check all the variables available to your template, you could use the debug template tag
{{ debug }}
Probably even better though is to use the django-debugging-toolbar - this will also let you examine the SQL queries your view is making.
Missing Modules
I would expect this to raise an exception if this were the problem, but have you checked that you have the psycopg module on your new machine?

django-selectable LookupAlreadyRegistered error

i am trying django-selectable the 'fruit' example given in the docs here -
https://django-selectable.readthedocs.org/en/version-0.6.2/quick-start.html#defining-a-lookup
getting LookupAlreadyRegistered error,there is a small note on this error,but i can't
figure out how to solve this.i understand that it has something to do with import statements, i am using django-1.3
i will provide more info if needed , but i am just using the same code as given in the example.
kindly help
Check the note in the link you've posted:
You should only register your lookup once. Attempting to register the same lookup class more than once will lead to LookupAlreadyRegistered errors. A common problem related to the LookupAlreadyRegistered error is related to inconsistant import paths in your project. Prior to Django 1.4 the default manage.py allows for importing both with and without the project name (i.e. from myproject.myapp import lookups or from myapp import lookups). This leads to the lookup.py file being imported twice and the registration code executing twice. Thankfully this is no longer the default in Django 1.4. Keeping your import consistant to include the project name (when your app is included inside the project directory) will avoid these errors.

django-debug-toolbar with django-cms and Django 1.3

I keep hitting an error when trying to use django-debug-toolbar and django-cms together.
"MpttMeta has no attribute 'class'"
I have a feeling it's something to do with the Mptt app bundled with Django CMS, but I'm not sure, and I've seen this on a few projects but I'm surprised I can't find a direct hit for the error message in Google so I thought I'd post here.
I've tried using latest released version of debug toolbar, also the develop branch, and also dcramer's fork, but it's making no difference. I'm on Django 1.3 and Django CMS 2.1.3.
Any ideas?
Thanks!
The problem is that django-debug-toolbar expects the MpttMeta class needs to be a 'new style' class, which is a fairly straightforward patch (line 33 in django-cms/publisher/mptt_support.py). Change:
class MpttMeta:
to
class MpttMeta(object):
In Django-CMS 2.1.3, they still have their own monkey-patched mptt bundled with Django-CMS. In the next release of Django-CMS will no longer bundle its own mptt and will instead rely on the independently developed package.
It could be any problem related to Django 1.3.
Django CMS 2.1.3 supports only 1.2.X branch: http://docs.django-cms.org/en/2.1.3/getting_started/installation.html#requirements
Jonas Obrist, Django CMS dev says "Maybe a minor version of
2.1 will add official 1.3 support"
Or you can put this in yours .... urls.py for example. Not in settings.py because project will not start.
from publisher.mptt_support import MpttMeta
if not hasattr(MpttMeta, '__class__'):
MpttMeta.__class__ = type
Caught AttributeError while rendering: class MpttMeta has no attribute '__class__'
I believe this had to do with the way the MPTTMeta class is loaded into the metaclass (MPTTModelBase) making it not have a class attribute.
A monkeypatch fix is to wrap the offending statement in django-debug-toolbar like so:
try:
text = "method %s on %s object" % (receiver.__name__, receiver.im_self.__class__.__name__)
except:
text = "method %s on %s object" % (receiver.__name__, type(receiver.im_self).__name__)
This changes the output slightly to become
method finish_mptt_class on classobj object
Clearly not a permanent fix, but it gets you the debug-toolbar + django-cms working.

Debugging cryptic "Error: cannot import name <Name>" on Django

Sometimes when I run manage.py I get a cryptic message in red that says Error: cannot import name <Name> and nothing else.
Obviously, this is a simple import or syntax error and with a little looking around, I can usually fix it. Other times however it takes me quite a while to figure out what exactly I did wrong. Is there a way to get Django to spit out more info in this situation?
This is an annoying problem. Luckily, it's been fixed (recently): see this ticket and this patch.
If you want to just hack your local django install (you're running under virtualenv or buildout, right?), change to the root of your django directory (the one with README, INSTALL, etc) and run this:
curl "https://code.djangoproject.com/changeset/17197?format=diff&new=17197" | patch -p3\
So, if you run django trunk > 17197, apply the patch to your django install (it applied to django 1.2 for me), or wait until django 1.4, you'll be able to do this:
./manage.py shell --traceback
And you'll get a full traceback instead of just the Error: cannot import ...
Voila!
this happens when a circular import appears, when one model is dependent on another and in turn they try and import each other while executing code.
You might want to paste the two models that are causing you issues.
That'll help us debug.
Also it tends to happen sometimes with signals so if you have a signals file please paste too.