Wagtail adding new pages - python

My current way of adding new pages to my wagtail site is the following:
Edit the installed apps to include my new page
INSTALLED_APPS = [
'home',
'anotherpage',
'newpage', # this is new
'wagtail.wagtailforms',
...
]
Copy the format of anotherpage to be my newpage
cp -rf anotherpage newpage
Edit my newpage/models.py references to anotherpage to be newpage.
E.g.
class Anotherpage(Page):
becomes
class Newpage(Page):
And the newpage/migrations/0001_initial.py references
Rename: mv feedback/templates/info feedback/templates/feedback
and mv feedback/templates/feedback/info_page.html feedback/templates/feedback/feedback_page.html
etc
Then run python manage.py makemigrations
Then python manage.py migrate
Question
This workflow feels quite inefficient, is there a better way to be doing any of the above?
I'm new to python and wagtail. Any insight on what I could be doing better would be greatly appreciated

You don't have to create a new INSTALLED_APPS entry and app folder for every new page model you create - a single models.py file can contain as many page models as you like. Generally I'd recommend one app for each major area of your site - for example, you could have a blog app containing BlogPage, BlogIndex and BlogArchive page types. You can even define the page models for your entire site in a single app if you want - although that can get hard to maintain when the file grows very large.
This way, creating a new page model just involves adding the class Newpage(Page): definition to the existing models.py, running ./manage.py makemigrations, and adding a new template into the existing template directory.
Additionally, when you do decide to create a new app, you can make use of the ./manage.py startapp some_app_name command to save having to create files and folders manually.

Related

How do you rename apps in Django1.11 LTS? [duplicate]

I have a Django app named app1 with models and migrations files.
I renamed this app to app2 and I fixed all imports, urls etc...
I now have a problem with migrations files and data in tables.
How can I write migrations with the correct way to ensure:
New installation => create the new tables
Update old versions => create new tables, move data, remove old tables
Note 1: there is several tables with many Foreign Keys.
Here is my progress so far and I am not sure if I am on the good way:
I removed all older migrations
I ran python manage.py makemigrations to generate new migrations files
After these 2 steps, I can install my application but I still have problems with old version.
Question: What is the best way to migrate data?
Note 2: I don't use South.
I found a solution that's works
Fix old migrations with new Foreign Keys and new app dependencies.
Force old migrations to create tables with old app name, so for that in migrations.CreateModel.options, add db_table: 'app1_table_name'
In each migration file add replaces = [('app1', 'migration_file_name')]. This will tell to Django that current migration (app2.migration_file_name) will replace the old file, this will prevenent django to execute migrations twice.
Create a migration file to rename tables with migrations.AlterModelTable
This is eloquently answered in this blog post.
The bullet points are:
Rename the folder of the application you want to update.
Update any and import statements to the folder you updated.
Update entries for the django_content_type table to refer to the application's app_label.
Update the table names of any models you haven't explicitly set the table name of. These table names inferred by the application name and need to be updated.
Update entries for the django_migrations table and update the reference for each migration by setting the app field your new app label.
Update any namespaced folder names that are within your /static or /templates folder. For example, you might have
./foo_app/templates/foo_app/index.html and it should be updated to ./bar_app/templates/bar_app/index.html.
Renaming an app is always a tricky issue.
If you do the migration like a simple table renaming migration, at any moment the apps.get_model() for the old app cannot work because the app simply doesn't exist.
I found this answer. I know you are not using south, but I think it might work the same way, just skip the south steps.
Basically, you have to:
Dump the data, before rename, into a json file
Run the script in the answer to rename references in the json file from the app1 to app2
Rename app1 to app2 (all import references, settings.py, etc)
Run the migrations to create the tables for app2
Load the data from json file to the database
Drop the app1 tables
I hope this help.

How to add page templates to Wagtail after installing into an existing Django application

I'm trying to extend an already existing Django App. The app is functioning fine as is, but I would like to add blog functionality.
I've installed Wagtail, using the guidelines here (http://docs.wagtail.io/en/latest/getting_started/integrating_into_django.html) To check wagtail is installed, I have navigated to here:
http://myurl/cms
And the wagtail admin panel is displayed. When I navigate to http://myurl/admin I get the default admin control panel for my Django app, so far so good.
Now I am trying to build the blog.
I found this tutorial:
http://wiseodd.github.io/techblog/2015/06/22/developing-wagtail/
which suggests the following as a first step: -
First, we’ll create our generic page class as home page class is
already created by default when we started Wagtail project.
It then displays this code:
# core/models.py
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailsearch import index
# We’re deriving our GenericPage from Page class, so that our GenericPage also has Page’s field, e.g. title
class GenericPage(Page):
# Let’s create our custom field, named body which is a rich text
body = RichTextField()
# Index the body field, so that it will be searchable
search_fields = Page.search_fields + (index.SearchField(‘body'),) # To show our body field in admin panel, we have to wrap it with FieldPanel and add it to Page’s field panel content_panels = Page.content_panels + [FieldPanel('body', classname=‘full’)]
I could not find which file I was meant to add this into. I searched the system using grep, and found a number of files that had the text string:
from wagtail.wagtailcore.models import Page
I decided the most likely candidate was in the directory:
env/lib/python2.7/site-packages/wagtail/project_template
Within my original app directory. I added the code above to the models.py file residing in the above directory. I then ran
python manage.py makemigrations
But it said no migrations were found. The next step in the tutorial posted above suggests you should now see three different page types available to create in the control panel, but I can not find the option to create any pages.
Can you tell me if I edited the correct file above, or if I should have edited a different file, and also
Why I am not seeing any option to add a new page in the wagtail control panel?
I have consulted with the documentation here (http://docs.wagtail.io/en/latest/getting_started/tutorial.html) and tried following the 'extend the homepage model' section, but couldn't figure out where the home/models.py file is as there is no folder called home in my Django app.
Thanks for any advice
As the final section of the "integrating into Django" docs says:
You’re now ready to add a new app to your Django project (via ./manage.py startapp - remember to add it to INSTALLED_APPS) and set up page models
Running ./manage.py startapp blog will add a blog app to your project, including an empty models.py - this is where you add your page definitions. (The Wagtail docs don't go into detail on this, because it's just following the standard Django workflow, which is hopefully familiar to anyone with an existing Django project to integrate with...)
Tutorials that use wagtail start my_project as a starting point will omit this step, because the starter project comes with a pre-made models.py with a HomePage model. The site-packages/wagtail/project_template directory you found is actually the 'master' copy of the starter project, which gets cloned at the point that you run wagtail start my_project. Since this isn't hooked up to your current project, changing it had no effect.

How to Edit a Database Table Following a Django App Name Change

I have just created my first Django app which uses a Postgres database... which was working until I broke it by trying to rename it. I only have placeholder data in the database, and so I am not concerned about database migration, I simply want to get my app working again and with an empty database is just fine. I found this question on here: How to change the name of a Django app? which lays out the steps involved in re-naming a Django app. I have done the first two which are to 1) Rename the folder found in the project root and 2) change any references to the app in its dependencies, i.e. the app's views, the urls.py and settings.py files.
As a newbie to Django and programming, the third step is not clear to me. The instructions were to:
"Edit the database table django_content_type with the following command: UPDATE django_content_type SET app_label='' WHERE app_label='' Note: for renaming models, you'll need to change django_content_type.name"
So I have two very specific questions:
1) Where/how do I edit the database table? As in, where do I physically type this command? And/or how do I change the "django_content_type.name"?
2) I have already pushed my project to Heroku and have a working version of my app there. What will I need to do on the Heroku side to handle this change?
Thanks in advance for your patience!
You need to go to the database table manually, or use dbshell.
> ./manage.py dbshell #opens the database command line.
This places you in the command line
$> UPDATE django_content_type SET app_label='<new_label>' WHERE app_label='<old_label>'
$> <ctrl>-D
This is make you all set. You dont need to do anything else on the heroku side apart from this change.

How to create a table and its related model.py automatically from a csv file in Django

Think of this:
You create a CMS of some sort, which asks you for an application name and a csv file for that application.
Then it automatically creates that app on the fly, creates the required model.py based on the csv columns, activates the admin page for it and allows only you to have the full permission to this new table via django admin, then it inserts the the app into the url.py and creates the view.py for it as well.
Then all you'd have to do is upload a csv, name your app and whola!, you have an admin page to play with.
Now, is there anyway to create an app or at least a model.py out of a csv file in django or is there any django-app that can do this?
Note: Look beyond (./manage.py inspectdb > models.py)
While this does not involve creating an actual models.py and application, you may want to look into dynamically creating Model classes at runtime. You could have "meta" models that store the information on the dynamic models, and then have your CSV view import the data into those models, create the classes, and register them with the admin. Or something like that.
Creating an actual application directory, with models.py, views.py, and so on, is fairly easy (just create the directory, create the files, and write formatted strings to them based on the CSV data). Editing the project's settings.py and urls.py, and reloading the modules, wouldn't be too difficult either. But, I wouldn't trust automatically generated Django applications without first looking at them.

Defining models in a top level Django directory

I've noticed that in order for me to define models, I need to do something like:
python manage.py startapp app_name
Is there anyway to avoid this convention and be able to create a models.py directly in the top level site that django-admin.py has created for me? Sometimes I'm building a site that can be put together in literally 15 minutes thanks to Django. I don't see the need for complexity of having an additional app built and a modified settings.py just for a form model or something similar.
Not really. The django admin programs expect app/models.py file names.

Categories

Resources