簡體   English   中英

如何將djangobb集成到現有項目中?

[英]How to integrate djangobb in an existing project?

wget https://bitbucket.org/slav0nic/djangobb_project/get/tip.tar.gz
tar zxvf tip.tar.gz
cd slav0nic-djangobb_project-tip/
pip install -r requirements.txt
cd basic_project/
touch local_settings.py
 #set DATABASE
 ./manage.py syncdb --all
 ./manage.py collectstatic
 ./manage.py runserver

這是djangobb支持上提到的安裝准則。 在安裝了requirements.txt之后,我被卡住了。 如何將djangobb集成到現有項目中。 Django noob在這里需要幫助。

在這里,您可以找到我兩個月前寫的指南。 現在,我看到該指南可以少走幾步,但它不會改變結果:),所以我看不出有理由重新編寫它。 閱讀指南后,如果您有任何疑問,請詢問。


目前DjangoBB由2個Git片段組成:

  • App本身的3個分支( stable ,default和bootstrap3)
  • 2個項目分支( 默認和dimka665 / *********)

在本教程中,我們將使用DjangoBB的粗體版本。

1)stable / default / botstrap3-表示DjangoBB_Forum作為App本身。 穩定分支具有最新版本的代碼,因此可以使用它。

資源

壓縮檔案

2)默認-Django的框架項目結構,其中包含啟動“ DjangoBB_Forum應用”所需的所有設置(urls.py,settings.py,模板等)。 這只是項目框架(類似於./manage.py startproject),此處不包含DjangoBB_Forum as App。

資源

壓縮檔案

讓我們下載兩個存檔,將其解壓縮,為方便起見,將我們擁有的兩個文件夾(均具有原始名稱“ slav0nic-djangobb-****”)重命名為DjangoBB_App(用於“ stable”應用程序的分支),並重新命名為DjangoBB_Project(用於“默認”項目的分支) 。 (我們將合並兩個檔案的文件\\數據)


安裝。

今天(19.09.2015)的最新版本的Django是1.8.4。 本教程也100%適用於1.8.2和1.8.3。 我尚未測試Django的早期版本。

現在,DjangoBB_Forum要求如下所示:

  • Django> = 1.6,<1.9(實際的最新穩定版本是1.8.4)

  • django-haystack> = 2.1.0,<2.4(本教程時的實際版本為2.4)

  • 枕頭> = 2.1.0(實際版本為2.9.0)
  • 郵戳(實際版本為1.2.2)
  • pygments(實際版本為2.0.2)
  • pytz> = 2015.4(這是實際版本)
  • django-pagination-py3 =​​= 1.1.1(此實際版本)
  • django-allauth(實際版本為0.23.0)
  • django-messages(實際版本是0.5.1)
  • django-nocaptcha-recaptcha(實際版本為0.0.18)
  • Whoosh(實際版本為2.7.0)

將DjangoBB_Forum集成到現有項目中的最大問題是設置,因為設置因用戶而異。 我以結構為例向您展示,准備了urls.py和settings.py,使您可以通過所有必要的說明輕松地將新設置集成到項目中。 在使用下面的settings.py之前,您需要使用數據庫設置更改那里的DATABASES部分。 在下面還有更多內容,您將看到第二個屏幕,其中包含文件夾\\文件的標簽,這些標簽向您解釋了settings.py中的更改,因為您確定還有另一個絕對路徑,也可能還有另一個相對路徑。

還需要提及的是,在屏幕上您會看到3個其他文件(default_settings.py,development.py,production.py),而不是“ settings / settings.py”文件。 在手冊中,說“ settings.py”是指您的“ settings.py”文件,而不是其在屏幕上的文件。

准備好接受djangobb_forum的項目的初始結構(app_shows_and_times和app_places僅用於使我們感覺到已經添加了djangobb_forum的現有項目): 為DjangoBB_Forum准備的初始項目講師

/src/bugaga/urls.py

"""bugaga URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))
"""

from django.conf.urls import *
from django.conf import settings
from django.contrib import admin
from django.conf.urls.static import static

from djangobb_forum import settings as forum_settings
from djangobb_forum.sitemap import SitemapForum, SitemapTopic


sitemaps = {
    'forum': SitemapForum,
    'topic': SitemapTopic,
}

urlpatterns = patterns('',
    # Admin
    url(r'^admin/', include(admin.site.urls)),

    # Sitemap
    url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}),

    #My_Apps
    url(r'^places/', include('app_places.urls')),
    url(r'^shows/', include('app_shows_and_times.urls')),

    # DjangoBB_Forum
    url(r'^forum/account/', include('allauth.urls')),
    url(r'^forum/', include('djangobb_forum.urls', namespace='djangobb')),
)

# PM Extension
if (forum_settings.PM_SUPPORT):
    urlpatterns += patterns('',
        url(r'^forum/pm/', include('django_messages.urls')),
   )

if (settings.DEBUG):
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

/src/bugaga/settings/development.py

# -*- coding: utf-8 -*-
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

DEBUG = True
TEMPLATE_DEBUG = DEBUG
#print ("base dir path", BASE_DIR)

ADMINS = (
    # ('Your Name', 'your_email@domain.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'name_of_db',
        'USER': 'login_to_db',
        'PASSWORD': 'pass_to_db',
        'HOST': 'localhost',
        'PORT': '',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'ru-RU'

LANGUAGES = (
    ('ca', 'Catalan'),
    ('cs', 'Czech'),
    ('de', 'German'),
    ('en', 'English'),
    ('es', 'Spanish'),
    ('fo', 'Faroese'),
    ('fr', 'France'),
    ('it', 'Italian'),
    ('lt', 'Lithuanian'),
    ('mn', 'Mongolian'),
    ('nl', 'Dutch'),
    ('pl', 'Polish'),
    ('ru', 'Russian'),
    ('uk_UA', 'Ukrainian'),
    ('vi', 'Vietnamese'),
    ('zh_CN', 'Chinese'),
)

SITE_ID = 1

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Europe/Kiev'
USE_TZ = True

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# STATIC_ROOT is where the static files get placed from STATIC_URL and STATICFILES_DIRS
# when they are collected by "manage.py collectstatic". Static files are used by Apache\nginx
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
#When you’re developing using Django’s development server, you won’t have anything to do with this setting.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = "/home/antonio/projects/bugaga.com/static/"

# URL prefix for static files in your apps
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# STATICFILES_DIRS is a setting you use to declare non app-specific static files
# You can prefixes for templates, as STATICFILES_DIRS = (("downloads", "/opt/webfiles/stats"),)
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
    #'/var/www/static/',
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
     'django.contrib.staticfiles.finders.FileSystemFinder', # is default; responsible for STATICFILES_DIRS
     'django.contrib.staticfiles.finders.AppDirectoriesFinder', # is default; responsible for $app_name/static/
     'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = '/home/antonio/projects/bugaga.com/media/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'YOUR_SECRET_KEY GENERATED BY DJANGO'

# Make this unique, and don't share it with anybody.

if not hasattr(globals(), 'SECRET_KEY'):
    SECRET_FILE = os.path.join(BASE_DIR, 'secret.txt')
    try:
        SECRET_KEY = open(SECRET_FILE).read().strip()
    except IOError:
        try:
            from random import choice
            import string
            symbols = ''.join((string.lowercase, string.digits, string.punctuation ))
            SECRET_KEY = ''.join([choice(symbols) for i in range(50)])
            secret = file(SECRET_FILE, 'w')
            secret.write(SECRET_KEY)
            secret.close()
        except IOError:
            raise Exception('Please create a %s file with random characters to generate your secret key!' % SECRET_FILE)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',

    #DjangoBB_Forum part
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'pagination.middleware.PaginationMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',

    'djangobb_forum.middleware.LastLoginMiddleware',
    'djangobb_forum.middleware.UsersOnline',
    'djangobb_forum.middleware.TimezoneMiddleware',
)

ROOT_URLCONF = 'bugaga.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # Directories where the engine should look for template source files, in search order.
        # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',

                #DjangoBB_Forum part
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',

                'django_messages.context_processors.inbox',
                #'allauth.account.context_processors.account', #not required since v0.21.0
                #'allauth.socialaccount.context_processors.socialaccount', #not required since v0.21.0

                'djangobb_forum.context_processors.forum_settings',
            ],
            #DjangoBB_Forum part
             #'loaders': [
             #    'django.template.loaders.filesystem.Loader', #is the same as DIRS [] not empty
             #    'django.template.loaders.app_directories.Loader', #is the same as APP_DIRS = True
             #    'django.template.loaders.eggs.Loader',
             #]
        },
    },
]

PREREQ_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MY_APPS = [
    'app_places',
    'app_shows_and_times',
]

DJANGOBB_APPS = [
    'django.contrib.sites', #required by django-allauth
    'django.contrib.sitemaps',
    'django.contrib.admindocs',
    'django.contrib.humanize',

    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.openid',
    #'allauth.socialaccount.providers.facebook', # at first you need to configure Facebook or
    # you will get Error: No Facebook app configured: please add a SocialApp using the Django admin
    'allauth.socialaccount.providers.google',
    'allauth.socialaccount.providers.twitter',
    'allauth.socialaccount.providers.vk',

    'pagination',
    'haystack',
    'django_messages',
    'nocaptcha_recaptcha',

    'djangobb_forum',
]

INSTALLED_APPS = PREREQ_APPS + MY_APPS + DJANGOBB_APPS

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

try:
    import mailer
    INSTALLED_APPS += ('mailer',)
    EMAIL_BACKEND = "mailer.backend.DbBackend"
except ImportError:
    pass

try:
    import south
    INSTALLED_APPS += ('south',)
    SOUTH_TESTS_MIGRATE = False
except ImportError:
    pass

FORCE_SCRIPT_NAME = ''

# Haystack settings
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
        'PATH': os.path.join(BASE_DIR, 'djangobb_forum/djangobb_index'),
        'INCLUDE_SPELLING': True,
    },
}
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

# Account settings
ACCOUNT_ACTIVATION_DAYS = 10
LOGIN_REDIRECT_URL = '/forum/'
LOGIN_URL = '/forum/account/login/'
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)

# Cache settings
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True

# Allauth
ACCOUNT_LOGOUT_ON_GET = True
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_SIGNUP_FORM_CLASS = 'bugaga.forms.SignupForm'

try:
    from local_settings import *
except ImportError:
    pass

(0)假設我們在VirtualEnvironment中的某個位置有project_name / src文件夾(應該已經安裝(自v3.4以來是Python的內置功能)),我們將其用作項目文件夾。

  1. 將全部內容從djangobb_project / basic_project / media / *復制到/bugaga.com/media/

  2. 將所有全部內容從djangobb_project / basic_project / templates / *復制到/bugaga.com/src/templates/

  3. 將djangobb_project / basic_project / forms.py復制到/bugaga.com/bugaga.com/src/settings/

  4. 從djangobb_app /復制以下內容:

    • 'djangobb_forum'文件夾
    • 'requirements.txt'文件
    • 'requirements_optional.txt'文件到/bugaga.com/bugaga.com/src/
  5. 現在,您應該具有下一個結構(用黑色箭頭標記的新內容)

集成了DjangoBB_Forum的項目結構

  1. 激活您的虛擬機(請參閱步驟0)

  2. cd到'/bugaga.com/bugaga.com/src /'(這是我的項目路徑)

  3. 運行'pip install -r requirements.txt'(pip應該也安裝很久了)

  4. 運行'pip install -r requirements_optional.txt'

  5. 運行'pip install django-nocaptcha-recaptcha'

  6. 運行'pip install whoosh'

  7. 在“ /bugaga.com/bugaga.com/src/”中創建文件“ secret.txt”,然后在其中放置任何您喜歡的隨機字符串,例如“ asd423llkmasdi2”

  8. 現在嘗試“ ./manage.py永遠運行”並打開http://127.0.0.1:8000/forum/ 如果出現以下錯誤:settings.DATABASES配置不正確。 請提供引擎值。 查看設置文檔以獲取更多詳細信息。 這意味着您需要在“ /bugaga.com/bugaga.com/src/settings/settings.py”中正確設置數據庫。 在方框中(默認情況下為平均值),我們有DB的下一個設置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

在使用PostgreSQL時,我可以為PostgreSQL提供數據庫模板:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'name_of_db',
        'USER': 'login_to_db',
        'PASSWORD': 'pass_to_db',
        'HOST': 'localhost',
        'PORT': '',
    }
}
  1. 如果您沒有看到上面的錯誤,那么您應該看到下面的錯誤:

    django.db.utils.ProgrammingError:關系“ djangobb_forum_forum”不存在第1行:...用戶”。“ is_active”,“ auth_user”。“ date_joined”來自“ djangobb _...”

:)

  1. 運行'./manage.py migration'

  2. 如果出現錯誤:

    django.db.utils.ProgrammingError:關系“ auth_user”不存在

->運行``./manage.py遷移身份驗證''

  1. 如果出現錯誤:

    psycopg2.ProgrammingError:關系“ django_site”不存在第1行:SELECT(1)AS“ a”來自“ django_site” LIMIT 1

->運行“ ./manage.py遷移網站”

  1. 運行“ ./manage.py migration”(它將所有其余應用程序一起遷移,因此您無需指定每個應用程序的名稱)。

  2. 運行'./manage.py makemigrations'

  3. 再次運行'./manage.py migration'

  4. 在瀏覽器中打開論壇之前,您需要擁有一個帳戶('./manage.py createsuperuser'),否則將會出現錯誤:

    • 在瀏覽器中:用戶匹配查詢不存在。
    • 在控制台中:django.contrib.auth.models.DoesNotExist:用戶匹配查詢不存在。
  5. 另外為了避免錯誤:

    ImportError:沒有名為“ allauth.account.context_processors”的模塊

->打開'bugaga.com/bugaga.com/src/settings/settings.py',並在TEMPLATE_CONTEXT_PROCESSORS部分中,注釋(按#)2行,如下所示:

  #  'allauth.account.context_processors.account',
  #  'allauth.socialaccount.context_processors.socialaccount',
  1. 現在我們可以打開我們的論壇,但是語言仍然有1個問題。 要修復此問題,請將cd更改為'/ src / djangobb_forum /'並運行'django-admin compilemessages'

  2. 現在您可以運行'./manage.py runserver'並歡迎使用DjangoBB_Forum http://127.0.0.1:8000/forum/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM