How can I use Flask Admin panel in a different package? - python

This is my application structure:
/blog
/blog
/app.py
models.py
views.py
/admin
__init__py
views.py
...
I want to use flask-admin extension in a different package.
in /admin/__init__.py I imported the app and flask-admin extension:
from flask.ext.admin import Admin
from app import app
then I initiate the admin app like that:
admin = Admin(app)
However, I get 404 error. Why? Should I use blueprint or what?

I assume you're trying to hit the default /admin routes within your Flask app for Flask admin?
My guess right now is that none of your code does import admin anywhere, which is probably good since admin's __init__.py will try to re-import your app.py all over again (from the from app import app reference) and you'll end up in a circular dependency.
What I'd do is alter app.py to contain the admin = Admin(app) and from flask.ext.admin import Admin code, and also do a from admin import views and empty out the admin/__init__.py file completely.

Related

How to register multiple apps models in a admin.py file

I have three app in my project first app does not have any model while the the second app has two models which have been registered in admin site as given bellow.
admin.py for second app:
from django.contrib import admin
from .models import Country, CountryDetails
admin.site.register(Country)
admin.site.register(CountryDetails)
My third app has another model and I want to register its model in the admin site of second app, how can I do this.
I have tried this, Though I don't know wither its a right way to do it or not but its not working.
from django.contrib import admin
from .models import Country, CountryDetails
from thirdapp.models import Model_of_my_third_app
admin.site.register(Country)
admin.site.register(CountryDetails)
admin.site.register(Model_of_my_third_app)
showing an Error
Django “No Module Named URLs” error
Thanks
After struggling little bit I am able to figure out the problem, Actually I din't create a "urls.py" files for my "thirdapp", while I had included the given line:
url(r'', include('fastaapp.urls')),
Into myproject urls.py file, hence I have to create a "urls.py" file within a new app with a minimum content like this:
urls.py for "thirdapp" should be like this
from django.conf.urls import url
from . import views
urlpatterns = [
]

how to update django admin

I have an app named polls in Project A,and it's in admin too, but I added an app named pingpong later, I added the app pingpong in the installed_app and synced database(it works), but the admin page is still the same. So how do I do to make the admin page update?
You have to add your models in admin.py
from pingpong.models import YourModel
admin.site.register(YourModel)

Testing Django allauth

I am trying to test my app but not sure how to configure the django-allauth in the test environment. I am getting:
ImproperlyConfigured: No Facebook app configured: please add a SocialApp using the Django admin
My approach so far is to instantiate app objects inside tests.py with actual Facebook app parameters, an app which functions correctly locally in the browser:
from allauth.socialaccount.models import SocialApp
apper = SocialApp.objects.create(provider=u'facebook',
name=u'fb1', client_id=u'7874132722290502',
secret=u'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
apper.sites.create(domain='localhost:8000', name='creyu.org')
How can I get these tests running? Thanks
Where inside tests.py do you instantiate this app object? If it's inside the setUpModule() method, there shouldn't be a problem.
Personally, I would create a fixture init_facebook_app.json with the relevant information and then inside tests.py (before the test cases) define:
from django.core.management import call_command
def setUpModule():
call_command('loaddata', 'init_facebook_app.json', verbosity=0)
This ensures that the data in the fixture are loaded before the tests are run, and that they are loaded only once, i.e. not before each test. See this for reference regarding call_command.
Lastly, posting your Facebook app secret key anywhere on the internet is not a good idea - I would reset it if I were you.
I would create a migration so all your environments have the data
eg
import os
from django.db import models, migrations
from django.core.management import call_command
from django.conf import settings
class Migration(migrations.Migration):
def add_initial_providers(apps, schema_editor):
import pdb;pdb.set_trace()
call_command(
'loaddata',
os.path.join(settings.BASE_DIR, 'fixtures/social_auth.json'),
verbosity=0)
dependencies = [
('my_app', '001_auto_20160128_1846'),
]
operations = [
migrations.RunPython(add_initial_providers),
]

Templates not found in Flask Blueprint

I'm working on an app using Blueprints and I followed the documentation quickstart on Blueprints, but still have a 404 on my templates.
I got an admin blueprint looking like that:
from flask import Blueprint, render_template
admin = Blueprint('admin', __name__, template_folder='templates')
#admin.route('/')
def admin_index():
return render_template('admin/index.html.jinja2')
As described in the doc, my template is in /myapp/admin/templates/index.html.jinja2+
I register the blueprint in my app __init__.py file:
from flask import Flask
from . import config
from admin import admin
app = Flask(__name__)
app.config.from_object(config.DevelopmentConfig)
# register my blueprint
app.register_blueprint(admin, url_prefix='/admin/')
If anyone has an idea about a mistake I could have done, please tell me!
You are not accurate about docs. Your templates shoud be in
myapp/admin/templates/admin/index.html.jinja2
Note admin folder in templates.

Making urls.py file for Flask like in Django

Maybe someone can help/explain me, how to create urls.py file for Flask like in Django?
main.py - main project file. It includes only app runner (app.run()).
urls.py is situated in the same directory and need to provide views
from views.py.
You can do this as is described in the Flask documentation, basically by calling app.add_url_rule to set your routes rather than using the decorator.
In addition to the Flask documentation, this can be solved like this:
When creating the Flask app load your 'urls.py' file
app.register_blueprint(apps.someapp.urls.mod)
Then structure urls.py as following:
from flask import Blueprint
from apps.someapp.views import SomeView
# set method as endpoint
view = SomeView.as_view('someview')
# Create the blueprint for this app
mod = Blueprint("payment_methods", __name__, url_prefix="/someapp/", template_folder="templates")
# Add the view as route; methods like GET, POST, PUT will automatically route to class methods with parameters
mod.add_url_rule('<int:id>/', view_func=view)

Categories