django-seo set up error - No module named seo - python

I am trying to add django-seo to a test project, but I have an error I don;t know how to solve.
I have followed the django-seo docs:
1. I ran pip install DjangoSEO and then confirmed the install with pip freeze;
2. I added 'rollyourown.seo' to my installed apps;
3. I created a file called seo.py and installed this file alongside my core files: models.py, views.py, forms.py & urls.py. This is the contents of my seo.py file:
from rollyourown import seo
class MyMetadata(seo.Metadata):
title = seo.Tag(head=True, max_length=68)
description = seo.MetaTag(max_length=155)
keywords = seo.KeywordTag()
heading = seo.Tag(name="h1")
4. I added the following code to my urls.py file to set up the admin:
from rollyourown.seo.admin import register_seo_admin
from django.contrib import admin
from test_project.seo import MyMetadata
register_seo_admin(admin.site, MyMetadata)
According to the docs, I should now be able to see the django-seo models in the admin, but do not see them.
Instead, I get the following error:
File "C:\Users\UserName\Desktop\test_project\test_project\core\urls.py", line 5, in <module>
from test_project.seo import MyMetadata
ImportError: No module named seo
Any suggestions would be great.

Related

Django context_processor no module named

I want to use a context_processors for my base.html template.
You can see my project structure in the following picture:
Project structure
from difoni_forms.DIFONIAPP.models import Admin
def check_active(request):
return {"active" : Admin.objects.get(id=1).active}
This is the code from my context_processors.py.And I added this line of code to my settings 'DIFONIAPP.context_processors.check_active'
When I run the program i get the following error:
No module named 'difoni_forms.DIFONIAPP'.
I don't understand what I am doing wrong any helpis appreciated, ty in advance.

Django Import Error: cannot import name "Shift"

I'm pretty sure it's due to a circular import but I'm not too sure how to solve my issue
in mysite/shifts/models.py
from django.db import models
class Shift(models.Model):
# defines a shift`
in mysite/shifts/scrapes.py
from shifts.models import Shift
I can share more code if you need but I believe the issue is here, I've added my apps to settings.py and I get this error when I run scrapes.py as a django-admin command
from shifts.models import Shift
ImportError: No module named 'shifts'
project_structure
Use the following line in mysite/shifts/scrapes.py
from .models import Shift
from mysite.shifts.models import Shift

Django on Pycharm community edition seems to show false-positive error message

I'm new to Django and I'm using Pycharm Community 2018.1 to do my project.
While I was doing the first tutorial where you set up a model and corresponding admin page, Pycharm keeps showing this reference error which is perfectly fine when you look at the directory and the structure.
Is this what I get for using free Pycharm instead of paid Professional edition?
However, I know I'm a newb so I might be wrong here. Is it normal to get this error message in this situation?
I took a screenshot of error message(the left screen) and working Admin page(right screen) and here are my codes for models.py and admin.py
models.py (there is no problem with it)
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
# Create your models here.
#python_2_unicode_compatible
class Bookmark(models.Model):
title = models.CharField(max_length = 100, blank = True, null = True)
url = models.URLField('url', unique = True)
def __str__(self):
return self.title
admin.py (you can see unresolved reference error)
from django.contrib import admin
from bookmark.models import Bookmark
# Register your models here.
class BookmarkAdmin(admin.ModelAdmin):
list_display = ('title', 'url')
admin.site.register(Bookmark, BookmarkAdmin)
This warning can be solved by opening your actual project directory
Go to File->Open. Then choose your project directory, that is mysite and you are opened pyneer_django directory
It's nothing with the community edition

Cannot import models from another app in Django

so I have 2 apps running in the same project.
My files are structured as follows:
/project_codebase
/project
__init.py
settings.py
urls.py
wsgi.py
...
/app1
...
/app2
...
manage.py
So, I for some weird reason have a different name for my base directory (that is, it ends with codebase). Hopefully, that is not an issue.
In my settings.py, I have this:
INSTALLED_APPS = [
...
'app1',
'app2',
]
Ok, so in my models.py (from app2), I can easily import models from app1 with from app1.models import *, however, when I use from app2.models import * in my models.py (from app1), I get an ImportError.
Any solutions to this?
This might be due to circular import issues. To avoid this you should load the model dynamically:
For recent versions of django (1.7+) use the application registry:
from django.apps import apps
MyModel1 = apps.get_model('app1', 'MyModel1')
For earlier django versions (<1.7):
from django.db.models.loading import get_model
MyModel1 = get_model('app1', 'MyModel1')
Note 1: If you want to define a ForeignKey relationship, there is no need for a separate import statement. Django has you covered on this:
If app1 is an installed app, you should define the ForeignKey relationship as follows:
# in app2.py
class MyModel2(models.Model):
mymodel1 = models.ForeignKey('app1.MyModel1')
Note 2: The get_model only works if app1 is an installed app and MyModel1 is the model you want to import from app1.
Note 3: Try to avoid wildcard import (from ... import *), as this is bad practice.
It's definitely a circular import.
But i think is what you need is to use models as some sort of RetationFields(ForeignKey, ManyToManyField or OneToOneField) arguments. So you need to skip import and use as so:
# app1/models.py
class Model1(models.Model):
relation_field = models.ForeignKey('app2.Model2')
From docs:
If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself
To refer to models defined in another application, you can explicitly specify a model with the full application label
Just put str object as first argument to relation fields that leeds to <app_name>.<Model_name>.
Note: it's better to avoid importing everything from module(from <module_name> import *)
If you want to import only some specific module then do not use import *.
It will take more time load your all library and so can affect the speed of your app also.
If you want to use few modules from your second app then just add module name instead of whole libraries something like this:
from app2.models import Module1, Module2
or it may be circular import issue as other clarify.
Thanks.
i use this code always and it's work :)
from position_app.models import Member
You need to specify the model names you want to import, for ex from app1.models import ModelName1, ModelName2.
Make sure there is no name clash between one of your apps and one of the modules installed in your Python environment. If you use pip, you can run pip freezeto see a list of installed modules.
I had the same error when one of my apps was named 'packaging', and the packaging python module was installed.
I also face this problem when I try to import my model from another app in (django2.2)
But at last I Imported It and Its successfully working.
here is my two app:
INSTALLED_APPS = [
...
'categories',
'videos',
]
and this is the code for how I Imported it into videos/models.py file as a ForeignKey Connectivity
from django.db import models
class Videos(models.Model):
categories = models.ForeignKey('categories.Categories', related_name='categories', on_delete=models.CASCADE)
If want to see my Categories Model from categories/models.py file, you can check this code otherwise neglect it
from django.db import models
class Categories(models.Model):
category_name = models.CharField(max_length=50)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
It is a circular import
In my case I needed the imported class not for a relation field, but for use it inside a method instead.
If that's the case, I suggest to import inside the method, otherwise an AppRegistryNotReady("Models aren't loaded yet.") is raised.
class Student(CustomUser):
""" Usuario alumno """
class Meta:
verbose_name = "Alumno"
verbose_name_plural = "Alumnos"
def get_current_school_class(self):
""" Obtiene el curso actual de un alumno """
from school.models import SchoolClass, StudentClass
# proceed with the method...
it's not necessary to import models from others apps
just put the app.models in the foreignkey field and that's work ;)
app 1:
class model1(models.Model):
field=models.field type ...
app 2:
class model2(models.Model):
field=models.ForeignKey('app1.model1', on_delete. ...)
to avoid code correction :D

auth is not available in module

I have a web2py application where I have written various modules which hold business logic and database related stuff. In one of the files I am trying to access auth.settings.table_user_name but it doesn't work and throws and error as global name 'auth' is not defined. If I write the same line in controller, it works. But I want it to be accessed in module file. Please suggest how do I do that.
In your model file, where you define auth:
from gluon import current
auth = Auth(db)
current.auth = auth
Then in a module:
from gluon import current
def some_function():
auth = current.auth
....
For more details, see http://web2py.com/books/default/chapter/29/04/the-core#Accessing-the-API-from-Python-modules.
I was getting a very similar error ("name 'auth' is not defined"). Had to add from django.contrib import auth at the top of views.py and it worked.

Categories

Resources