I changed a bit wagtail 0001_initial.py and 0002_chreate_homepage.py and if i do migrate/makemigrations i get Server Error
Here is how i changed these files
0001_initial.py
# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-02-11 16:32
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('wagtailcore', '0040_page_draft_title'),
]
operations = [
migrations.CreateModel(
name='HomePage',
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')),
('tournament_section_title', models.CharField(blank=True, help_text='Title to display above the next matches', max_length=255, null=True)),
('matches_section_title', models.CharField(blank=True, help_text='Title to display above the next matches', max_length=255, null=True)),
('news_section_title', models.CharField(blank=True, help_text='Title to display above the News section on Home page', max_length=255, null=True)),
('presentation_screen_section_title', models.CharField(blank=True, help_text='Title to display above the News section on Home page', max_length=255, null=True)),
('matches_section', models.ForeignKey(blank=True, help_text='Choose a page to link to for the Matches Page', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailcore.Page', verbose_name='Choose ')),
('news_section', models.ForeignKey(blank=True, help_text='Choose a page to link to for the News Page.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailcore.Page', verbose_name='News')),
('presentation_screen_section', models.ForeignKey(blank=True, help_text='Choose a page to link to for the Presentation Screen Page.', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailcore.Page', verbose_name='Presentation Screen')),
('tournament_section', models.ForeignKey(blank=True, help_text='Choose a page to link to for the Matches Page', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailcore.Page', verbose_name='Choose ')),
],
options={
'abstract': False,
},
bases=('wagtailcore.page',),
),
]
and 0002_chreate_homepage.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
def create_homepage(apps, schema_editor):
# Get models
ContentType = apps.get_model('contenttypes.ContentType')
Page = apps.get_model('wagtailcore.Page')
Site = apps.get_model('wagtailcore.Site')
HomePage = apps.get_model('base.HomePage')
# Delete the default homepage
# If migration is run multiple times, it may have already been deleted
Page.objects.filter(id=2).delete()
# Create content type for homepage model
homepage_content_type, __ = ContentType.objects.get_or_create(
model='homepage', app_label='home')
# Create a new homepage
homepage = HomePage.objects.create(
title="Home",
draft_title="Home",
slug='home',
content_type=homepage_content_type,
path='00010001',
depth=2,
numchild=0,
url_path='/home/',
)
# Create a site with the new homepage set as the root
Site.objects.create(
hostname='localhost', root_page=homepage, is_default_site=True)
def remove_homepage(apps, schema_editor):
# Get models
ContentType = apps.get_model('contenttypes.ContentType')
HomePage = apps.get_model('base.HomePage')
# Delete the default homepage
# Page and Site objects CASCADE
HomePage.objects.filter(slug='home', depth=2).delete()
# Delete content type for homepage model
ContentType.objects.filter(model='homepage', app_label='home').delete()
class Migration(migrations.Migration):
dependencies = [
('base', '0001_initial'),
]
operations = [
migrations.RunPython(create_homepage, remove_homepage),
]
Which way I have to do this, so if I deploy my project to heroku and run migrate it will automatically create HomePage that i changed.
my Project tree
-Project/
|-requirements
|-treichle-cup
|-base
|-search
|-settings
|-static
|-templates
In site settings my HomePage is exist and set as Root Page
Related
I am trying to upload webpages to my django server.
They are all project of mine, and I want to be able to add more projects in the future through the admin panel:
I am working in an app called projects
This is the model I am using:
from django.db import models
from django.utils.timezone import now
from django.core.files.storage import FileSystemStorage
# Create your models here.
class Project(models.Model):
class ProjectType(models.TextChoices):
PYTHON = 'Python'
JAVASCRIPT = 'Javascript'
REACTJS = 'React.js'
REACTNATIVE = 'React Native'
JAVA = 'Java'
C = 'C'
CPP = 'C++'
def upload_location_photo(instance, filename):
return f'photos/projects/{instance.slug}/{filename}'
def upload_location_template(instance, filename):
#I want to get into, app: projects, folder: templates/projects
return f'projects/templates/projects/{instance.slug}/{filename}'
def upload_location_static(instance, filename):
#I want to get into, app: projects, folder: static/projects
return f'projects/static/projects/{instance.slug}/{filename}'
slug = models.CharField(max_length=200, unique=True)
project_type = models.CharField(max_length=50, choices=ProjectType.choices, default=ProjectType.JAVASCRIPT)
title = models.CharField(max_length=150)
description = models.TextField(blank=True)
date_completed = models.DateTimeField(default=now, blank=True)
photo_main = models.ImageField(upload_to=upload_location_photo)
photo_1 = models.ImageField(upload_to=upload_location_photo, blank=True)
photo_2 = models.ImageField(upload_to=upload_location_photo, blank=True)
photo_3 = models.ImageField(upload_to=upload_location_photo, blank=True)
#FILE UPLOAD OF JS APPS
file_html = models.FileField(upload_to=upload_location_template, max_length=100, blank=True)
file_css = models.FileField(upload_to=upload_location_static, max_length=100, blank=True)
file_js = models.FileField(upload_to=upload_location_static, max_length=100, blank=True)
This is in a django app called projects.
This issue is, that the html, css, and js files are being uploaded into: media/projects/static/projects and media/projects/templates/projects
instead of going into my app, they are being saved in the global media folder, how can I stop this, and direct them into my app's template and static folder?
Sorry, I asked that question too soon, however now I can help someone else hopefully!
I needed to add a few lines of code:
new imports:
import os
from django.core.files.storage import FileSystemStorage
from django.conf import settings
Adjust my upload location functions:
def upload_location_template(instance, filename):
return f'{instance.slug}/{filename}'
def upload_location_static(instance, filename):
return f'{instance.slug}/{filename}'
Create new storage locations, and add them as arguments to my FileFields:
template_storage = FileSystemStorage(location=os.path.join(settings.BASE_DIR, 'projects/templates/projects/'))
static_storage = FileSystemStorage(location=os.path.join(settings.BASE_DIR, 'projects/static/projects/'))
file_html = models.FileField(upload_to=upload_location_template, storage=template_storage, max_length=100, blank=True)
file_css = models.FileField(upload_to=upload_location_static, storage=static_storage, max_length=100, blank=True)
file_js = models.FileField(upload_to=upload_location_static, storage=static_storage, max_length=100, blank=True)
It is now working perfectly, used this answer here: https://helperbyte.com/questions/177113/django-multiple-media-root
I'm trying to create a new model, i named it products, and i found this error message which you see below
Note that my django version is 1.8.7
##urls.py:
from django.contrib import admin
from mySecondGdgBlogs import views
from django.urls import include, path
from myGdgBlogs import views
urlpatterns = [
path('admin/', admin.site.urls),
path('writeafterme/',include('mySecondGdgBlogs.urlss')),
path('htm/', views.htamal),
path('dp/',views.dpmain)
]
##views.py:
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def dbmain(request):
data = ''
return HttpResponse(data)
##models.py
from django.db import models
# Create your models here.
class Products(models.Model):
name = models.CharField (max_length=20)
price = models.IntegerField()
type=models.CharField(max_length=20,choices=(
('S', 'Small'),
('M', 'Medium'),
('L', 'Large'),
))
class Meta:
db_table = 'Products'
I expected to show me a white page but the actual result in the browser is :
No module named 'django.urls'
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.8.7
Exception Type: ModuleNotFoundError
Exception Value:
No module named 'django.urls'
Exception Location: /home/mohammed/PycharmProject/GdgProject/myGdgProject/myGdgProject/urls.py in <module>, line 7
Python Executable: /usr/bin/python3
Python Version: 3.6.8
Python Path:
[
'/home/mohammed/PycharmProject/GdgProject/myGdgProject',
'/usr/local/lib/python3.6/dist-packages/pip-19.1.1-py3.6.egg',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/home/mohammed/.local/lib/python3.6/site-packages',
'/usr/local/lib/python3.6/dist-packages',
'/usr/lib/python3/dist-packages'
]
Im trying to add an image to my polls app, which i have set up as: upload_to='mysite/static/polls_app/question_pics'
but the wrong file path is used when I view the page:
GET /polls/1/static/polls_app/question_pics/
How can I go about editing this so Django uses the url where the image is saved?
Models.py
question_image = models.ImageField(upload_to='static/polls_app/question_pics', default=None, blank=True, null=True)
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
Views.py
model = Question
template_name = 'polls_app/detail.html'
detail.html
<img src="{{ question.question_image.url }}" alt="image">
urls.py
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
Add below in urls.py
from django.views.static import serve
from django.urls import include,re_path
from . import settings
// Your code as it is
urlpatterns += [re_path(r'^media/(?P.*)', serve, {'document_root': settings.MEDIA_ROOT})]
This should help !!
In my blog post model I am trying to migrate a RichTextField to a StreamField. I have followed the Wagtail docs "Migrating RichTextFields to StreamField" including the section on migrating blog posts with revisions. They were not effective. How do I turn a RichTextField into a StreamField?
This is for a blog using Django 1.11.13, Wagtail 2.1 and PostgreSQL. I have over 200 blog posts, many of them with the Live+Draft status meaning they have unpublished revisions. I inspected the blog posts in the database, it looks like their body fields are stored as HTML.
I copied over the code from the docs and changed all references to relate to my own project. Upon running migrate, I got an AttributeError that "raw_text" is not found. So I created an exception to pass over it. I applied the migration and it completed with an OK.
Then in models.py I changed my class's body attribute from a RichTextField to a StreamField with a RichFieldBlock. I also changed its content panel from a FieldPanel to a StreamFieldPanel. I applied this migration and it completed with an OK.
When I viewed some posts in Wagtail admin, all the posts with a Live+Draft status were converted to RichTextBlocks inside StreamFields, however, their content was wrapped inside a JSON object called {'rich_text': ''}. The JSON object was not styled like the rest of the text inside the editor. When I viewed those posts live no data showed up, I assume because the template could't read JSON. All the blog posts with a Live status also had the RichTextField converted to StreamField, but their content was empty. Their data was erased from the editor. When I viewed them live they were blank. However, when I inspect them in the database their body fields still contain the previous HTML that I saw.
This is a Live+Draft post in admin:
This is a Live post in admin:
I tried to install a fresh copy of the database after I ran the two migrations and was seeing odd data, and that didn't improve things.
template.html:
<section>
{{ page.body }}
</section>
models.py before I ran the conversion migration:
class BlogPost(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body'),
]
migration.py, I added an exception for the AttributeError within the page_to_streamfield() function because raw_text was not found:
# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2019-05-01 13:46
from __future__ import unicode_literals
import json
from django.core.serializers.json import DjangoJSONEncoder
from django.db import migrations, models
from wagtail.core.rich_text import RichText
def page_to_streamfield(page):
changed = False
try:
if page.body.raw_text and not page.body:
page.body = [('rich_text', {'rich_text': RichText(page.body.raw_text)})]
changed = True
except AttributeError:
pass
return page, changed
def pagerevision_to_streamfield(revision_data):
changed = False
body = revision_data.get('body')
if body:
try:
json.loads(body)
except ValueError:
revision_data['body'] = json.dumps(
[{
"value": {"rich_text": body},
"type": "rich_text"
}],
cls=DjangoJSONEncoder)
changed = True
else:
# It's already valid JSON. Leave it.
pass
return revision_data, changed
def page_to_richtext(page):
changed = False
if page.body.raw_text is None:
raw_text = ''.join([
child.value['rich_text'].source for child in page.body
if child.block_type == 'rich_text'
])
page.body = raw_text
changed = True
return page, changed
def pagerevision_to_richtext(revision_data):
changed = False
body = revision_data.get('body', 'definitely non-JSON string')
if body:
try:
body_data = json.loads(body)
except ValueError:
# It's not apparently a StreamField. Leave it.
pass
else:
raw_text = ''.join([
child['value']['rich_text'] for child in body_data
if child['type'] == 'rich_text'
])
revision_data['body'] = raw_text
changed = True
return revision_data, changed
def convert(apps, schema_editor, page_converter, pagerevision_converter):
BlogPage = apps.get_model("blog", "BlogPost")
for page in BlogPage.objects.all():
page, changed = page_converter(page)
if changed:
page.save()
for revision in page.revisions.all():
revision_data = json.loads(revision.content_json)
revision_data, changed = pagerevision_converter(revision_data)
if changed:
revision.content_json = json.dumps(revision_data, cls=DjangoJSONEncoder)
revision.save()
def convert_to_streamfield(apps, schema_editor):
return convert(apps, schema_editor, page_to_streamfield, pagerevision_to_streamfield)
def convert_to_richtext(apps, schema_editor):
return convert(apps, schema_editor, page_to_richtext, pagerevision_to_richtext)
class Migration(migrations.Migration):
dependencies = [
# leave the dependency line from the generated migration intact!
('blog', 'previous_migration'),
]
operations = [
migrations.RunPython(
convert_to_streamfield,
convert_to_richtext,
),
]
models.py after running the previous migration, I manually changed it to a StreamField and ran a second migration for just this change:
class BlogPost(Page):
body = StreamField([
('rich_text', blocks.RichTextBlock())
], blank=True)
content_panels = Page.content_panels + [
StreamFieldPanel('body'),
]
I expected to see a blog post's data inside a StreamField within Wagtail admin, but instead it was blank or wrapped in a JSON object.
I was able to migrate a RichTextField to StreamField with a RichTextBlock with this script (this assumes a schema that looks like the first 3 chapters of the Wagtail Getting Started tutorial). I found that it was easier to think about this process by breaking it into distinct steps: fresh db from backup/make backup, schema migration, data migration, and admin/template alterations. I found that I needed to loop through each BlogPost and all of its associated PageRevision. Editing the live published data was straightforward, but the drafts are stored as serialized JSON two levels deep, which was tricky to figure out how to interact with. Hopefully this script helps others. Note: this script doesn't migrate in reverse.
0004_convert_data.py
import json
from django.db import migrations
import wagtail.core.fields
from wagtail.core.rich_text import RichText
def convert_data(apps, schema_editor):
blog_page = apps.get_model('blog', 'BlogPage')
for post in blog_page.objects.all():
print('\n', post.title)
# edit the live post
if post.body.raw_text and not post.body:
post.body = [('paragraph', RichText(post.body.raw_text))]
print('Updated ' + post.title)
post.save()
# edit drafts associated with post
if post.has_unpublished_changes:
print(post.title + ' has drafts...')
for rev in post.revisions.all():
data = json.loads(rev.content_json)
body = data['body']
print(body)
print('This is current JSON:', data, '\n')
data['body'] = json.dumps([{
"type": "paragraph",
"value": body
}])
rev.content_json = json.dumps(data)
print('This is updated JSON:', rev.content_json, '\n')
rev.save()
print('Completed ' + post.title + '.' + '\n')
class Migration(migrations.Migration):
dependencies = [
('blog', '0003_blogpage_stream'),
]
operations = [
migrations.AlterField(
model_name='blogpage',
name='body',
field=wagtail.core.fields.StreamField([('paragraph', wagtail.core.blocks.RichTextBlock())], blank=True),
),
migrations.RunPython(convert_data),
]
models.py
from django.db import models
from wagtail.core.models import Page
from wagtail.core import blocks
from wagtail.core.fields import RichTextField, StreamField
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.images.blocks import ImageChooserBlock
from wagtail.search import index
class BlogIndexPage(Page):
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full")
]
class BlogPage(Page):
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
# body = RichTextField(blank=True)
body = StreamField([
('paragraph', blocks.RichTextBlock()),
], blank=True)
stream = StreamField([
('heading', blocks.CharBlock(classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
], blank=True)
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
FieldPanel('date'),
FieldPanel('intro'),
StreamFieldPanel('body'),
StreamFieldPanel('stream'),
]
templates/blog/blog_page.html
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block body_class %}template-blogpage{% endblock %}
{% block content %}
<h1>{{ page.title }}</h1>
<p class="meta">{{ page.date }}</p>
<div class="intro">{{ page.intro }}</div>
{{ page.body }}
<p>Return to blog</p>
{% endblock %}
I'm new to wagtail and I'm creating a site with react.js as the frontend and wagtail as the backend. I'm primarily using wagtail's API and axios to render content to react. That said, I'm having issues getting the full absolute URL from wagtail's API to render images. For example, the get response when making a call for the URL is "/media/images/ayoba-desk.2e16d0ba.fill-1500x1500.png". How do I get the absolute URL (i.e. 127.0.0.1:8000/../../)?
Model.py
from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.core.models import Page
from django.db.models import TextField
from rest_framework import serializers
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel, InlinePanel
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.images.api.fields import ImageRenditionField
from wagtail.search import index
from wagtail.api import APIField
class ProjectsPage(Page):
# db fields
project_h_one = models.CharField(max_length=250, default="Project Name")
project_h_two = models.CharField(
max_length=250, default="Project Description")
project_intro_p = models.TextField(blank=True)
project_h2_date = models.CharField(
max_length=250, default="Project Launch Date")
project_p = models.TextField(blank=True)
project_h2_tech_stack = models.CharField(
max_length=250, default="Tech Stack")
project_tech_stack_description = RichTextField(blank=True, features=["ul"])
project_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
# Search index configuration
search_fields = Page.search_fields + [
index.SearchField('project_h_one'),
index.FilterField('project_h_two'),
index.FilterField('project_intro_p'),
index.FilterField('project_h2_date'),
index.FilterField('project_p'),
index.FilterField('project_h2_tech_stack'),
index.FilterField('project_tech_stack_description'),
]
# Editor panels configuration
content_panels = Page.content_panels + [
FieldPanel('project_h_one'),
FieldPanel('project_h_two', classname="full"),
FieldPanel('project_intro_p', classname="full"),
FieldPanel('project_h2_date', classname="full"),
FieldPanel('project_p', classname="full"),
FieldPanel('project_h2_tech_stack', classname="full"),
FieldPanel('project_tech_stack_description', classname="full"),
ImageChooserPanel('project_image'),
]
promote_panels = [
MultiFieldPanel(Page.promote_panels, "Common page configuration"),
]
# API configuration
api_fields = [
APIField('project_h_one'),
APIField('project_h_two'),
APIField('project_intro_p'),
APIField('project_h2_date'),
APIField('project_p'),
APIField('project_h2_tech_stack'),
APIField('project_tech_stack_description'),
APIField('project_image'),
APIField('project_img', serializer=ImageRenditionField(
'fill-1500x1500', source='project_image')),
]