I cannot understand where stored keywords and urls.
For example in module website exist class website_seo_metadata. It has such columns:
'website_meta_title': fields.char("Website meta title", translate=True),
'website_meta_description': fields.text("Website meta description", translate=True),
'website_meta_keywords': fields.char("Website meta keywords", translate=True),
When I added some keywords for page records not exist in DB.
I can not understand where stored urls for pages. I know, if I create route:
#http.route('/contacts/', type='http', auth="public", website=True)
this path was added to sitemap.xml
For generate sitemap.xml used sitemap_xml_index method:
#http.route('/sitemap.xml', type='http', auth="public", website=True)
def sitemap_xml_index(self):
current_website = request.website
cr, uid, context = request.cr, openerp.SUPERUSER_ID, request.context
ira = request.registry['ir.attachment']
iuv = request.registry['ir.ui.view']
But in this tables not present any one url. I can not understanding how it generate path for all pages. Where is it data stored?
I wrote own module but pages from it not present in sitemap.xml and keywords not correctly saved and displayed.
I tried extend my models such as:
class pr_info_pages(models.Model):
_name = 'pr_filials.pr_info_pages'
_inherit = ['mail.thread', 'website.seo.metadata', 'website.published.mixin']
but nothing changed...
How i can add my urls to sitemap and normally store keywords?
For keywords you need use in model:
_inherit = ['mail.thread', 'website.seo.metadata']
Also, when you generate page data you need put to page 'main_object'. This is the dictionary element that contains the object of your model. For example:
return http.request.render(_your_template_, {
'page_data': page_data,
'main_object': _object_
})
Related
I have a model form similar to the one below:
class BookSearchForm(ModelForm):
class Meta:
model = Book
fields = ['publisher', 'authors', 'category'
How to override fields name attribute in the above model form?
I tried this, but it did not work:
class BookSearchForm(ModelForm):
class Meta:
model = Book
fields = ['publisher', 'authors', 'category'
widgets = {
'publisher': forms.SelectMultiple(attrs={'name': 'pub'}),
'authors': forms.SelectMultiple(attrs={'name': 'aut'}),
'category': forms.SelectMultiple(attrs={'name': 'cat'}),
}
you don't want to change the name of a field in the forms, django needs that to collect data for that field if you don't provide db_column as the name of the db column. what you can do with the first option, if you want the user to see publisher or some other name, in the models, add a verbose_name and then for the actual name you can declare the field however you want. Your code could look like this
pub = models.WhateverField(verbose_name='what i want you to see')
now when you do {{form.pub.label)}}, 'what i want you to see' is displayed in the html. Of course, don't forget to add the actual input in you template, {{form.pub}}. This way, you don't add anything extra in the form to display a user friendly name. I've posted this as an answer as i ran out of characters for a commment.
I'm have a django app with the following model.
class Posts(models.Model):
post = models.TextField(max_length=1500)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
likes = models.ManyToManyField(User, related_name="post_likes")
category = models.CharField(max_length=50, default=None)
To allow the users search for a particular blog and then display those objects I have the following view.
def index(request):
if request.method == "GET":
search = request.GET.get("input")
posts = Posts.objects.filter(category__icontains=f"{search}")
else:
posts = Posts.objects.all()
params = {'blog': posts}
return render(request, 'index.html', params)
In the above view , variable search contains what user has searched for, that I'm receiving through a form.
But I also want to provide a sort functionality after user has searched for a blog. Like user searches for "recipes", I need to show them all the search results and then let him sort them based on likes and recent etc. How do I do it since I'm losing search terms just after displaying the results.
I'm assuming the sort options are also part of the search form so you could also send the chosen sort option as a query parameter to the backend, just as you're doing with the search term:
sort = request.GET.get('sort')
# use the sort options to order your queryset as you desire
I'm trying to post data from a form that contains incomplete data (I set the missing data in the View class before saving) for the following model. But the form does not get submitted as it is invalid (it's missing the harvest_amount, but I set the value on the webpage before submitting.
class Harvest(models.Model):
harvest_amount = models.IntegerField(validators=[MinValueValidator(limit_value=0)])
harvest_date = models.DateField()
harvest_for_plant = models.ForeignKey(Plant, on_delete=models.CASCADE)
and my form
class HarvestCreationForm(forms.ModelForm):
class Meta:
model = Harvest
fields = [
'harvest_amount'
]
def is_valid(self):
//check if Id in the url contains a valid id for a plant
return True
In this case I forgot to migrate my changes to the model (where a field was deleted). When the form posted the data it would always hit the not null constraint since the deleted field was not being set.
I'm working on legacy code which is in Django (1.11)
I have a model A, with attributes:
Model_A:
Name (NOT NULL)
City (NOT NULL)
FieldX (Nullable) - CharField
And a model B, with attributes:
Model_B:
Name (NOT NULL)
City (NOT NULL)
RelatedField (ForeignKey to an instance of Model_A)
Now, When I add a record for Model_A then I may NOT need to fill FieldX.
However, When I add a record for Model_B then I'll have to select an instance of Model_A and then if FieldX of that instance is NULL then I have to fill that as well (make it mandatory).
The form for Model_A is pretty straight forward.
But for Model_B I need a form where:
First an instance of Model_A is selected (Dropdown)
The input box for FieldX of instance selected in 1 is shown (Editable and mandatory to fill, blank=False).
The rest of the fields are shown (Name, City, FieldY).
Can this be done using the admin page? Or will I have to create proper forms and user flow for this?
I have not tested this, but you should be able to do the following:
from django.contrib import admin
class ModelAForm(ModelForm):
FieldX = CharField(
required=True
)
class Meta:
model = Model_A
fields = ['FieldX']
class ModelAInline(admin.StackedInline):
model = Model_A
form = ModelAForm
class ModelBAdmin(admin.ModelAdmin):
inlines = [
ModelAInline
]
admin.site.register(ModelB, ModelBAdmin)
For model B I'd override the view and form in order to achieve this in admin site.
model_b_view = ModelBView.as_view()
#admin.register(Model_B)
class Model_B_Admin(admin.ModelAdmin):
def get_urls(self):
urls = super().get_urls()
my_urls = [
url(r'^add/$',
self.admin_site.admin_view(model_b_view)),
]
return my_urls + urls
Where ModelBView is a template view, and you'd have to override get and post of it, In get send all instances of Model_A to template and handle via jquery there.
I have got list of content items , for each content items I need to add a meta keyword and descriptions. I am using VandelayIndustries module for this. I have got list of keywords and descriptions along with the contentItemId .I can find out the published ContentItem from ContentItemVersionRecord.
Is there a way I can directly insert record in Vandelay_Industries_MetaRecord table with Id as ContentItemId and keywords and Descriptions.
You will need to create an import method. Your ContentItems should have a MetaPart. You can achieve this either by going to Content Definition in the Orchard dashboard or by modifing the ContentType in the migrations.
ContentDefinitionManager.AlterTypeDefinition("MyContentType",
type => type
.WithPart("MetaPart"));
Create a method that iterates thru the csv file and will add the entries:
foreach(var entry in listFromFile) {
var item = _contentManager.Get(entry.Id).As<MediaPart>();
item.Key = entry.Key;
item.Description = entry.Description;
}
And that's it.