How to use multiple forms on 1 wagtail page? - django-forms

Is it possible to create multiple forms on 1 page using wagtail? The documentation has many examples with 1 form, but not 1 with two or more. The documentation says
form_page.html differs from a standard Wagtail template in that it is
passed a variable form, containing a Django Form object, in addition
to the usual page variable.
But what if I need more than 1 form?
I don't know how to solve this with wagtail
class FormField(AbstractFormField):
name = models.CharField(max_length=50)
page = ParentalKey('FormPage', on_delete=models.CASCADE, related_name='form_fields')
class FormPage(AbstractEmailForm):
intro = RichTextField(blank=True)
thank_you_text = RichTextField(blank=True)
content_panels = AbstractEmailForm.content_panels + [
FieldPanel('intro', classname="full"),
InlinePanel('form_fields', label="Form fields"),
FieldPanel('thank_you_text', classname="full"),
MultiFieldPanel([
FieldRowPanel([
FieldPanel('from_address', classname="col6"),
FieldPanel('to_address', classname="col6"),
]),
FieldPanel('subject'),
], "Email"),
]

Related

Google Sitemap issue Contao 4.4

I am using contao 4.4 instance.I have a problem in google sitemap generation.
I have a newsletter page (page type = regular ) . In that page I have some newsletter articles (with teaser) . When I generate the sitemap, the url of these articles generated twice. When I checked the core I found a class which creates the page array for generating sitemap
vendor/contao/core-bundle/src/Resources/contao/classes/Backend.php
line no 662 - 680 .
Which append 'articles/' to the articles with teaser. So the sitemap generates url
with articles/
List item
without articles/
The first one is the correct url.Second Url generate 404. How I fix the issue ?
My siteconfiguration is as follows
->created a regular page with hidden in navigation and created articles with configuration show teaser
->created another page and created elements as 'teaser articles' and select articles from the above page
So your site structure is like this?
Page 1 with multiple articles
Page 2 with one article containing an „article teaser“ element, this page should be excluded from the sitemap
Is that correct? If yes this might be the solution:
In your „site structur“ you can EDIT Page 2: Scroll down to „Expert settings“, there is an option „Show in sitemap“ and you can select „Never show“.
I resolved the issue.
The url's were regenerated from the newsletter-bundle. In core bundle, the url of the article with teaser is generated. The newsletter-bundle is also contain hook for creating searchable page array. The hook regenerates the url. I wrote a function in vendor/contao/newsletter-bundle/src/Resources/contao/classes/Newsletter.php to check whether the array contain any duplicate url.
Modified function getSearchablePages() ( line 889-897
)
if($this->checkValidUrl($arrPages, $objItem, $strUrl)){
$arrPages[] = sprintf($strUrl, ($objItem->alias ?: $objItem->id));
}
Defining new function for finding duplicate
public function checkValidUrl($arrPages, $objItem, $strUrl)
{
$alias = $objItem->alias ?: $objItem->id;
$urlExplode = explode('%s', $strUrl);
$articleUrl = $urlExplode[0] . 'articles';
foreach ($arrPages as $arrPage) {
$validarticleUrl = $articleUrl . '/' . $alias;
if (strcasecmp($arrPage,$validarticleUrl) == 0) {
return false;
}
}
return true;
}

How to load initial data into ModelMultipleChoiceField - Django 1.6

I'm creating my first site using Django and having trouble loading initial data into a ModelForm linked to the built in Django group table.
Right now, users can go to a group page and select from an array of groups they would like to join. When they return to the page later, they see the same list of options/checkboxes again with no indication of which groups they already belong to.
I can't figure out how to have the intial group data load into the form, such that if you are already a member of "group 1" for example, that checkbox is already checked. I would also like to have it so that you could uncheck a box, and so when you submit the form you could be leaving some groups and joining others at the same time. Any help appreciated! My code below:
class GroupForm(ModelForm):
groupOptions = forms.ModelMultipleChoiceField(queryset=Group.objects.all(), label = "Choose your groups",
widget=forms.CheckboxSelectMultiple())
class Meta:
model = Group
fields = ['groupOptions']
def groupSelect(request):
if request.method == 'POST':
form = GroupForm (request.POST)
if form.is_valid():
group = form.cleaned_data['groupOptions']
request.user.groups = group
return render (request, 'groups/groupSelect.html' , {'form':form})
else:
form = GroupForm()
return render (request, 'groups/groupSelect.html' , {'form':form})
Took me a few days and some trial and error, but figured this one out on my own. Just needed to modify the second to last line in the code above. The ModelForm loads all available groups as options, and the line below causes the groups the user already belongs to to be checked.
form = GroupForm(initial={ 'groupOptions': request.user.groups.all() })

Creating Title String in sitefinity

I am trying to create a unique title in sitefinity, the current title is something like this: "Chicken Sandwich". This is the code that is creating the title:
Page.Title = (this._Product.PageTitle != "" ) ? this._Product.PageTitle : this._Product.Name;
I want to title to be something like this "Nutritional Information for Chicken Sandwich".
In Sitefinity 4/5 you kind of have 2 different titles. The first one is the 'name' of the page, the second is the Title for search engines. The name is seen throughout the backend of Sitefinity and is used in the navigation widgets. See screenshot here
You can retrieve both by the following statements:
string shorttitle = new PageManager().GetPageNode(new Guid(SiteMapBase.GetCurrentProvider().CurrentNode.Key)).Title;
and
string longtitle = new PageManager().GetPageNode(new Guid(SiteMapBase.GetCurrentProvider().CurrentNode.Key)).Page.HtmlTitle;
Hope it helps!
Jochem

Django-nonrel sorting by foreignkey

Is there a way of returning items from a database in django-nonrel, using 'order_by' on a foreignkey?
Full details are as follows:
#Models.py
class Post(models.Model):
article = models.TextField(help_text='Paste or type HTML in here')
pub_date = models.DateField()
....
class TagItems(models.Model):
title = models.CharField(max_length=200)
....
class TagRel(models.Model):
the_post = models.ForeignKey('Post')
the_tag = models.ForeignKey('Tag')
TagRel defines a ManytoMany relationship between Post and TagItems classes.
I am wanting to get a list of articles for each tag.
#Desire output
My tag
-my first post
-my second post
My second tag
- my other post
- another post
All is good so far, as I use the following to filter the data:
def tagged_posts():
tag_items = TagItems.objects.all()
li =[]
for item in tag_items:
tag_rel_item = TagRel.objects.filter(the_tag__pk = item.pk)
li.append(tag_rel_item)
return {'list_of_objects': li}
I am using db-indexer to define the filter part of the query in db-indexes.py. All this works fine but I want to order my posts by publication dates.
Django docs tell me to use:
TagRel.objects.filter(the_tag__pk = item.pk).order_by('the_tag__pub_date')
But the order_by('the_tag__pub_date') part does not appear to be supported by django-nonrel.
The following also works in normal Django:
TagRel.objects.filter(the_tag__pk = item.pk).order_by('the_post')
This works because the Posts are already sorted by date in the model.
But this also does not appear to work in django-nonrel.
So my question is how do I return my posts ordered by date (latest>oldest)?
Thanks in advance
I'm taking a guess at this - you're using a ManyToManyField. I believe that's implemented using a ListProperty on App Engine's datastore.
See the section in the datastore documentation labeled "Properties With Multiple Values Can Have Surprising Behaviors":
http://code.google.com/appengine/docs/python/datastore/queries.html
That's most likely why your results appear unsorted. ManyToMany relations aren't supported natively in GAE. You'd probably have to sort them yourself after you get the results back.

Display categories

I have my website working on social engine, I have a Main page named as articles.tpl, and in that I have a sub-page named as article.tpl, article.tpl is used to show the entire article, and articles.tpl is used to show topics of articles not the content,
My concern is with articles.tpl(pretty confusing articles and article)
In articles.tpl, 3 articles title, author of article and date is displayed, but I also want to display the category of that article,
For that I tried the following code,
$article_category = "";
$parent_category = "";
$article_category_query = $database->database_query("SELECT articlecat_id, articlecat_title, ".
"articlecat_dependency FROM se_articlecats WHERE articlecat_id='".
$rc_article->article_info[article_articlecat_id]."' LIMIT 1");
if($database->database_num_rows($article_category_query) == 1) {
$article_category_info = $database->database_fetch_assoc($article_category_query);
$article_category = $article_category_info[articlecat_title];
if($article_category_info[articlecat_dependency] != 0) {
$parent_category = $database->database_fetch_assoc(
$database->database_query("SELECT articlecat_id, articlecat_title".
" FROM se_articlecats WHERE articlecat_id='".
$article_category_info[articlecat_dependency]."' LIMIT 1"));
}
}
$smarty->assign('article_category', $article_category);
and in articles.tpl file I called it in this way
<span class="tahoma11_blue">| {$article_category}</span>
But when I check I get nothing, that space is blank, I am just able to see
|
How can I display the category?
i think you have to change
$smarty->assign('categories', $categories);
to
$smarty->assign('article_category', $article_category);
(in your example, you assign the (apparently empty) php variable $categories to the smarty variable categories. then, in the template, you use a smarty variable article_category - which never got assigned)

Resources