I've create a separate file for admin models - how do I get django to look in the file? At the moment, the courses are not displayed in admin. Here's admin.py:
from django.contrib import admin
from website.apps.courses import Course
class CourseAdmin(admin.ModelAdmin):
list_display = ('title', 'start_date')
admin.site.register(Course, CourseAdmin)
Here's urls.py:
from django.contrib import admin
from django.conf.urls.defaults import patterns, include, url
from website.apps.courses.views import *
admin.autodiscover()
urlpatterns = patterns('',
# Admin panel and documentation:
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
This is now solved. I hadn't imported the models from the admin.py file.
Related
I'm trying to import a custom module called 'clusterer.py' into my colab notebook. But the import function is not able to find the file. Is there any other way to import custom modules?
After mounting the drive, I have already tried this approach: How to import custom modules in google colab? with the result: 'No module named 'clusterer''
!ls /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/*.py
!cat /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/clusterer.py
import sys
sys.path.append('/content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection')
import clusterer
The output is as follows:
'/content/gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection/clusterer.py'
'/content/gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection/feature_extractor.py'
[Contents of the module 'clusterer.py']
ModuleNotFoundError: No module named 'clusterer'
Ok, I think I figured out one solution. In my case, I had to physically go inside the directory where the files were. This is what I did
1a. Restart the kernel but 'resetting all runtimes', specially if you've just added your file.py to the directory.
1b.cd gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection
!ls /content/gdrive/My\ Drive/Colab\Notebooks/Omdena_Mars_Anomaly_Detection/*.py
!cat /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/mylib.py
import sys
sys.path.append('/content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection')
5.import clusterer
Worked for me.
Thanks
I found myself written tedious code when importing files into dart files like the following:
import '../../constants.dart';
I'm wondering if there is any way to create an alias to specific folder like:
import '#shared/constants.dart';
Thanks,
Javi.
Dart doesn't allow you to rename imported identifiers, but it allows you to specify an import prefix
import '../../constants.dart' as foo;
...
foo.ImportedClass foo = foo.ImportedClass();
It allows also to filter imported identifiers like
import '../../constants.dart' show foo hide bar;
See also
https://www.dartlang.org/guides/language/language-tour#libraries-and-visibility
What is the difference between "show" and "as" in an import statement?
Barrel files can also make importing easier like
lib/widgets/widgets.dart
export 'widget1.dart';
export 'widget2.dart';
export 'widget3.dart';
export 'widget4.dart';
lib/pages/page1.dart
import '../widgets/widgets.dart';
Widget build(BuildContext context) => Widget1();
No. Dart do not have import alias.
But you have absolute imports which makes up for it:
import 'package:my_lib/shared/constants.dart
This will import the file /lib/shared/constants.dart
I import a package and would forward a member of it. What syntax does Dart offer for that?
foo.dart
import 'package:xzy/xzy.dart'; // contains class Xyz
// how do I forward Xyz here to make it available in bar.dart?
bar.dart
import 'package:mypackage/foo.dart';
Xyz is hidden here
You can use export
import 'package:xzy/xzy.dart';
export 'package:xzy/xzy.dart' show Xyz;
or just
export 'package:xzy/xzy.dart' show Xyz;
You don't need to import for that. Just export is enough if you don't need Xyz in this re-exporting file.
See also
https://www.dartlang.org/guides/libraries/create-library-packages
What is the difference between "show" and "as" in an import statement?
According to this tutorial, when defining routes in Angular for Dart you need to import the template.dart files of the relevant components:
import 'crisis_list_component.template.dart' as clct;
import 'hero_list_component.template.dart' as hlct;
I have put my components in a subfolder of the folder where this code resides. I have the import code in lib/src/routes.dart:
import 'components/foobar/foobar_component.template.dart' as fct;
and a Foobar component in lib/src/components/foobar/foorbar_component.dart.
When I have the foobar_component.dart code in src (i.e. the same folder as routes.dart) there's no problem. But when I move it to its own subfolder, I get the error:
Unable to find modules for some sources [...] Please check the following imports: import 'components/foobar/foobar_component.template.dart'
How do I get it to find the component in its subfolder?
You could use
import '../../components/foobar/foobar_component.template.dart' as fct;
but usually, it is better to not use ../ in imports and instead use package imports
import 'package:my_package/web/src/components/foobar/foobar_component.template.dart' as fct;
where my_package is the string used in name: ... in your pubspec.yaml and lib/ is skipped in the path to the imported file.
I've been looking around all week and can't wrap my head around how to get an instance of our pylons server started for use with the Behave BDD. Can any of you point me to an example or offer one of your own? Here's what I'm working with:
From the Tutorial page on the Behave doc site, starting a simple server and using selenium, this is their example code for Behave's features/environment.py:
import threading
from wsgiref import simple_server
from selenium import webdriver
from my_application import model
from my_application import web_app
def before_all(context):
context.server = simple_server.WSGIServer(('', 8000))
context.server.set_app(web_app.main(environment='test'))
context.thread = threading.Thread(target=context.server.serve_forever)
context.thread.start()
context.browser = webdriver.Chrome()
def after_all(context):
context.server.shutdown()
context.thread.join()
context.browser.quit()
def before_feature(context, feature):
model.init(environment='test')
It's a start, but I have no idea how to marry this to how our 'pylowiki' flavor of pylons is started up. There is another example I've found that may provide better clues, it is here. In this example I assume I'd replace "world" with "context", but aside from that the way the server is started in this example is different from how our own environment.py starts things up.
One last thing I think may be handy is to include the code from our current environment setup:
pylowiki/config/environment.py
# -*- coding: utf-8 -*-
"""Pylons environment configuration"""
import os
from mako.lookup import TemplateLookup
from pylons import config
from pylons.error import handle_mako_error
from sqlalchemy import engine_from_config
import pylowiki.lib.app_globals as app_globals
import pylowiki.lib.helpers
from pylowiki.config.routing import make_map
from pylowiki.model import init_model
def load_environment(global_conf, app_conf):
"""Configure the Pylons environment via the ``pylons.config``
object
"""
# Pylons paths
root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
paths = dict(root=root,
controllers=os.path.join(root, 'controllers'),
static_files=os.path.join(root, 'public'),
templates=[os.path.join(root, 'templates')])
# Initialize config with the basic options
config.init_app(global_conf, app_conf, package='pylowiki', paths=paths)
config['routes.map'] = make_map()
config['pylons.app_globals'] = app_globals.Globals()
config['pylons.h'] = pylowiki.lib.helpers
# Create the Mako TemplateLookup, with the default auto-escaping
config['pylons.app_globals'].mako_lookup = TemplateLookup(
directories=paths['templates'],
error_handler=handle_mako_error,
module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
input_encoding='utf-8', output_encoding='utf-8', default_filters=['escape'],
imports=['from webhelpers.html import escape'])
# Setup the SQLAlchemy database engine
engine = engine_from_config(config, 'sqlalchemy.')
init_model(engine)
# CONFIGURATION OPTIONS HERE (note: all config options will override
# any Pylons config options)
behave documentation says you need environment.py in feature folder, at least, and steps folder accrdingly
https://behave.readthedocs.io/en/latest/gherkin.html#controlling-your-test-run-with-tags