django

package
v1.7.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2026 License: GPL-2.0 Imports: 44 Imported by: 17

Documentation

Index

Constants

View Source
const (
	HOOK_SERVER_ERROR    = "django.ServerError" // ran when an exception is caught with err of type [except.ServerError]
	HOOK_SERVER_STARTUP  = "django.ServerStart" // ran just before django's http(s) server(s) start listening.
	HOOK_SERVER_SHUTDOWN = "django.ServerQuit"  // ran after django's http(s) server(s) shutdown.
	HOOK_SETUP_NOSURF    = "django.SetupNosurf" // ran during http server startup, allows for changing CSRF handler options
)
View Source
const (

	// Run the application in debug mode
	APPVAR_DEBUG = "DEBUG" // bool

	// Allowed hosts for the application, e.g. ["*"] - if the host is not in this list, the application will return an error when serving.
	APPVAR_ALLOWED_HOSTS = "ALLOWED_HOSTS" // []string (e.g. ["*"])

	// Use recoverer middleware for the application to recover from panics
	APPVAR_RECOVERER = "RECOVERER" // bool (use recoverer middleware)

	// Host address to bind the application's server to, e.g. "localhost"
	APPVAR_HOST = "HOST" // string

	// Port to bind the application's server to, e.g. "8080"
	APPVAR_PORT = "PORT" // string

	// The URL to serve static files from, e.g. "/static/"
	APPVAR_STATIC_URL = "STATIC_URL" // string

	// The port to bind the application's server to for TLS, e.g. "443"
	APPVAR_TLS_PORT = "TLS_PORT" // string

	// The path to the certificate file for TLS
	APPVAR_TLS_CERT = "TLS_CERT" // /path/to/cert.pem

	// The path to the key file for TLS
	APPVAR_TLS_KEY = "TLS_KEY" // /path/to/key.pem

	// A custom TLS configuration for the application
	APPVAR_TLS_CONFIG = "TLS_CONFIG" // *tls.Config

	// The database connection for the application
	APPVAR_DATABASE = "DATABASE" // drivers.Database

	// Continue running the application after executing cli- commands
	APPVAR_CONTINUE_AFTER_COMMANDS = "CONTINUE_AFTER_COMMAND" // bool

	// Log all routes that are accessed
	APPVAR_ROUTE_LOGGING_ENABLED = "ROUTE_LOGGING_ENABLED" // bool

	// Log static routes that are accessed
	APPVAR_STATIC_ROUTE_LOGGING_ENABLED = "STATIC_ROUTE_LOGGING_ENABLED" // bool

	// Wether the webserver is behind a proxy
	// This is so the application knows to use different headers to, for example
	// get the remote address of the client
	APPVAR_REQUESTS_PROXIED = "REQUESTS_PROXIED" // bool

	// The session manager for the application
	APPVAR_SESSION_MANAGER = "SESSION_MANAGER" // *scs.SessionManager

	// Disable nosurf middleware for the application
	APPVAR_DISABLE_NOSURF = "DISABLE_NOSURF" // bool

	// APPVAR_TRANSLATIONS_DEFAULT_LOCALE
	APPVAR_TRANSLATIONS_DEFAULT_LOCALE = "TRANSLATIONS_DEFAULT_LOCALE" // string
)
View Source
const ErrServeCanceled errs.Error = "Serve cancelled, signal hijacked response"

ErrServeCanceled is an error that can be returned from a signal to indicate that the serve should be cancelled.

It can be used to hijack the response and return a custom response.

This signal will be sent before most middleware has been executed.

Variables

View Source
var (
	Global       *Application
	AppInstalled = Global.AppInstalled
	Reverse      = Global.Reverse
	Static       = Global.Static
	Task         = Global.Task
)
View Source
var DEFAULT_LOGGING_ENABLED = true

Functions

func APPVAR_ErrorCode added in v1.7.2

func APPVAR_ErrorCode(code int) string

func AppLogger

func AppLogger(w logger.Log) func(*Application) error

func AppSettings

func AppSettings(settings Settings) func(*Application) error

func Apps

func Apps(apps ...any) func(*Application) error

func ConfigGet

func ConfigGet[T any](s Settings, key string, default_ ...T) T

func ConfigGetOK

func ConfigGetOK[T any](s Settings, key string, default_ ...T) (T, bool)

func Configure

func Configure(m map[string]interface{}) func(*Application) error

func ContextDataStore added in v1.7.2

func ContextDataStore(requestOrContext any) map[any]interface{}

func ContextDataStoreDelete added in v1.7.2

func ContextDataStoreDelete(requestOrContext any, key string) bool

func ContextDataStoreGet added in v1.7.2

func ContextDataStoreGet(requestOrContext any, key any, defaultValue ...interface{}) interface{}

func ContextDataStoreGetOK added in v1.7.2

func ContextDataStoreGetOK(requestOrContext any, key any) (interface{}, bool)

func ContextDataStoreMiddleware added in v1.7.2

func ContextDataStoreMiddleware(next mux.Handler) mux.Handler

ContextDataStoreMiddleware is a middleware that provides a ContextDataStore for each request.

This is a global datastore, on a per request basis - the underlying datastructure is a map[string]interface{}.

This allows for some flexibility with caching from lower context levels.

func ContextDataStoreSet added in v1.7.2

func ContextDataStoreSet(requestOrContext any, key any, value interface{})

func ContextWithApp added in v1.7.2

func ContextWithApp(ctx context.Context, app AppConfig) context.Context

func Flag

func Flag(flags ...AppFlag) func(*Application) error

func GetApp

func GetApp[T AppConfig](name string) T

func GetIP added in v1.7.2

func GetIP(r *http.Request) string

func IsStaticRouteRequest added in v1.7.2

func IsStaticRouteRequest(r *http.Request) bool

IsStaticRouteRequest checks if the request is marked as a static route request.

It checks the request context for a value of type `*bool` with the key [staticRouteKey]{}.

func MarkStaticRouteMiddleware added in v1.7.2

func MarkStaticRouteMiddleware(next mux.Handler) mux.Handler

MarkStaticRouteMiddleware marks the request as a static route request.

This is used to skip certain middleware for static routes, such as the logger middleware.

It should be used in conjunction with IsStaticRouteRequest to check if the current route is marked as a static route.

This should be used for route handlers, not as a global mux middleware. This middleware should be executed before any other middleware that needs to check if the route is static, which can be done by adding the middleware using mux.Route.Preprocess.

func NonStaticMiddleware added in v1.7.2

func NonStaticMiddleware(middleware mux.Middleware) mux.Middleware

NonStaticMiddleware is a middleware that skips the middleware if the request is a static route request.

This is useful for middleware that should not be executed for static routes, such as the logger middleware.

func RequestSignalMiddleware

func RequestSignalMiddleware(next mux.Handler) mux.Handler

RequestSignalMiddleware is a middleware that sends signals before and after a request is served.

It sends SIGNAL_BEFORE_REQUEST before the request is served and SIGNAL_AFTER_REQUEST after the request is served.

The signal it sends is of type *core.HttpSignal.

This can be used to initialize and / or clean up resources before and after a request is served.

func RequestWithStaticMark added in v1.7.2

func RequestWithStaticMark(r *http.Request, isStatic bool) *http.Request

RequestWithStaticMark returns a new request with the static route mark set to the given value.

It does so by adding a value to the request context using http.Request.WithContext.

The value is stored as a pointer to a bool - this is done to ensure that middleware that runs after this can modify the value if needed.

Types

type AppConfig

type AppConfig interface {
	// The application name.
	//
	// This is used to identify the application.
	//
	// An application cannot be registered twice - the name MUST be unique.
	Name() string

	// Dependencies for the application.
	//
	// This can be used to define dependencies for the application.
	//
	// All of theses dependencies must be registered before the application is initialized.
	Dependencies() []string

	// Models is used to define the models for the app.
	//
	// These models can then be used throughout the go-django application.
	Models() []attrs.Definer

	// Commands for the application.
	//
	// These commands can be used to run tasks from the command line.
	//
	// The commands are registered in the Django command registry.
	Commands() []command.Command

	// All apps have been initialized before OnReady() is called.
	OnReady() error

	// Check is used to check the application for any issues.
	Check(ctx context.Context, settings Settings) []checks.Message

	// BuildRouting is used to define the routes for the application.
	// It can also be used to define middleware for the application.
	//
	// A Mux object is passed to the function which can be used to define routes.
	BuildRouting(mux mux.Multiplexer)

	// Initialize your application.
	//
	// This can be used to retrieve variables / objects from settings (like a database).
	//
	// Generally we recommend you use this method for your applications
	// as opposed to doing stuff in toplevel init().
	//
	// Depending on the order of the registered applications, apps can depend on one- another.
	//
	// For example, this is used internally for authentication.
	//
	// I.E.: The 'sessions' app must always be registered before 'auth' in order for the auth app to work.
	Initialize(settings Settings) error

	Processors() []func(ctx.ContextWithRequest)

	Templates() *tpl.Config
}

AppConfig is the interface that must be implemented by all Django applications.

The AppConfig interface is used to define the structure of a Django application.

It can be used to define routes, middleware, templates, and other options / handlers.

The implementation of this interface can be found in [./src/apps/apps.go].

func AppFromContext added in v1.7.2

func AppFromContext(ctx context.Context) (AppConfig, bool)

func GetAppForModel added in v1.7.2

func GetAppForModel(model attrs.Definer) (AppConfig, bool)

type AppFlag

type AppFlag uint64
const (
	FlagSkipDepsCheck AppFlag = 1 << iota
	FlagSkipCmds
	FlagSkipChecks
)

type Application

type Application struct {
	Settings Settings
	Apps     *orderedmap.OrderedMap[string, AppConfig]

	Mux      *mux.Mux
	Log      logger.Log
	Commands command.Registry
	// contains filtered or unexported fields
}

The global application struct.

This is a singleton object.

It can only be configured once, any calls to the initialization function will return the old instance.

This allows for any registered application to freely call the initializer function to work with the application instance.

The application object should only be initialized once by calling `(*Application).Initialize()`

func App

func App(opts ...Option) *Application

func (*Application) App

func (a *Application) App(name string) AppConfig

func (*Application) AppInstalled

func (a *Application) AppInstalled(name string) bool

func (*Application) Config

func (a *Application) Config(key string) interface{}

Config returns the value of the key from the settings Shortcut for Application.Settings.Get(key)

func (*Application) Flagged

func (a *Application) Flagged(flag AppFlag) bool

func (*Application) Initialize

func (a *Application) Initialize() error

func (*Application) Quit

func (a *Application) Quit() error

func (*Application) Register

func (a *Application) Register(apps ...any)

func (*Application) Reverse

func (a *Application) Reverse(name string, args ...any) string

func (*Application) Serve

func (a *Application) Serve() error

func (*Application) ServerError

func (a *Application) ServerError(err error, w http.ResponseWriter, r *http.Request)

func (*Application) Static

func (a *Application) Static(path string) string

func (*Application) Task

func (a *Application) Task(description string, fn func(*Application) error) error

type DjangoHook

type DjangoHook func(*Application) error

type Locale added in v1.7.2

type Locale = string

type NosurfSetupHook

type NosurfSetupHook func(app *Application, handler *nosurf.CSRFHandler)

type Option

type Option func(*Application) error

type ServerErrorHook

type ServerErrorHook func(w http.ResponseWriter, r *http.Request, app *Application, err except.ServerError)

type Settings

type Settings interface {
	Set(key string, value interface{})
	Get(key string) (any, bool)
	Bind(app *Application) error
	App() *Application
}

func Config

func Config(m map[string]interface{}) Settings

type Translation added in v1.7.2

type Translation = string

type Untranslated added in v1.7.2

type Untranslated = string

Directories

Path Synopsis
contrib
admin
templ: version: v0.3.1020
templ: version: v0.3.1020
admin/chooser
templ: version: v0.3.1020
templ: version: v0.3.1020
admin/components
templ: version: v0.3.1020
templ: version: v0.3.1020
admin/components/columns
templ: version: v0.3.1020
templ: version: v0.3.1020
admin/components/menu
templ: version: v0.3.1020
templ: version: v0.3.1020
blocks
templ: version: v0.3.1020
templ: version: v0.3.1020
editor
templ: version: v0.3.1020
templ: version: v0.3.1020
editor/features
templ: version: v0.3.1020
templ: version: v0.3.1020
pages
templ: version: v0.3.1020
templ: version: v0.3.1020
Default signals for authentication events.
Default signals for authentication events.
ctx
pagination
templ: version: v0.3.1020
templ: version: v0.3.1020
widgets
templ: version: v0.3.1020
templ: version: v0.3.1020
list
templ: version: v0.3.1020
templ: version: v0.3.1020

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL