Add a prefix do Django comment form - django

I would like to add a prefix to each django comment form. I'm using multiply comment forms in the same page and depsite it's working well, i don't like having many input fields with the same id attribute like <input type="text" name="honeypot" id="id_honeypot" />.
So, is there a way to tell django do add a prefix to each form instance? I know i can do it with other forms when i create a form instance in this waynewform = CustomForm(prefix="a") but using Django's comment system, this part is handled by a comment template tag {% get_comment_form for [object] as [varname] %}.
Can I tell to the template tag to add a prefix?

Well, I have an idea. Add your custom comments form and override __init__. You can generate prefix from target_object and set it to self.prefix:
def __init__(self, target_object, data=None, initial=None):
...
Or better, override BaseForm.add_prefix:
def add_prefix(self, field_name):
"""
Returns the field name with a prefix appended, if this Form has a
prefix set.
Subclasses may wish to override.
"""
return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name
Update:
Yes, you're right. Prefix wouldn't work, the main reason is the code in contrib.comments.views.comments.post_comment. So I've reread your question and if you only need to change "id" attribute use BaseForm.auto_id:
class CustomCommentForm(CommentForm):
def __init__(self, target_object, data=None, initial=None):
super(CustomCommentForm, self).__init__(target_object, data, initial)
idprefix = target_object.__class__.__name__.lower()
self.auto_id = idprefix + "_%s"

Related

Django admin inline: Clone entry

I`m using a GenericStackedInline in my django admin. I would like to have the option to duplicate the inline object when editing the parent object.
I can think of two ways of doing this:
Using django-inline-actions to add a "clone" button. This did not work, because it does not show when using a fieldset in the GenericStackedInline
Adding another checkbox next to "delete" checkbox with label "clone". When activating the checkbox and saving parent object, it should clone the inline object with new id. Is there an easy way to add another checkbox and add a action to handle cloning?
Ok, found a solution. I don`t really like it. Biggest flaw in my oppinion is, that I did not find a way to do clone in InlineClass, Mixin or formset. It's a bit clumsy that two classes / Mixins need to be combined to handle it.
Improvements or other solutions are very welcome.
Inline classes
class RgCloneInlineFormset(BaseGenericInlineFormSet):
def add_fields(self, form, index):
super().add_fields(form, index)
form.fields["CLONE"] = forms.BooleanField(required=False)
class RgGenericStackedInlineWithClone(GenericStackedInline):
template = "admin/edit_inline/stacked_with_clone.html"
formset = RgCloneInlineFormset
class DetailpageElementInline(RgGenericStackedInlineWithClone, RgImagePreviewMixin):
model = DetailpageElement
extra = 0
Admin class
class CloneDetailpageElementInlineMixin():
def save_formset(self, request, form, formset, change):
# Save formset
ret = super().save_formset(request, form, formset, change)
# Do clone if nessesary
detailpage_ids_to_clone = []
for data in formset.cleaned_data:
if isinstance(data.get("id"), DetailpageElement):
if data.get("CLONE"):
detailpage_ids_to_clone.append(data.get("id").id)
for detailpage_element in DetailpageElement.objects.filter(id__in=detailpage_ids_to_clone):
detailpage_element.id = None
detailpage_element.save()
# Make sure function works like super function
return ret
#admin.register(ArticleInt)
class ArticleIntAdmin(CloneDetailpageElementInlineMixin, ContentAdminBase):
inlines = [
inlines.DetailpageElementInline,
]
Template admin/edit_inline/stacked_with_clone.html
Just copied the stacked.html from django source code and added
{% if inline_admin_formset.has_add_permission and inline_admin_form.original %}<span class="clone">{{ inline_admin_form.form.CLONE }} Clone</span>{% endif %}

django modelform property hidden field

I'm manually displaying my formset as a table, with each form being looped over. At the bottom of each form I include the hidden fields like:
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
But the problem is that I am also including properties in my form like:
class AllocationForm(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={'size': '15'}))
def __init__(self, *args, **kwargs):
super(AllocationForm, self).__init__(*args, **kwargs)
if self.instance:
self.fields['total_budgeted'] = self.instance.total_budgeted()
self.fields['total_budgeted_account_percent'] = self.instance.total_budgeted_account_percent()
self.fields['actual_spent'] = self.instance.actual_spent()
self.fields['actual_spent_account_percent'] = self.instance.actual_spent_account_percent()
self.fields['total_budgeted_category_percent'] = self.instance.total_budgeted_category_percent()
self.fields['actual_spent_category_percent'] = self.instance.actual_spent_category_percent()
class Meta:
model = Allocation
exclude = {'created', 'modified', 'source_account'}
And this works in the sense that I definitely see the properties being called, however they display as nothing so that's another issue.
The problem is when I keep the hidden fields in the template I will get errors such as 'int' object has no attribute 'get_bound_field' and so on depending on the return type of the property/method call.
My question is first: is there a check I can do to see if the field is a property in the template and therefore skip over it?
It may have something to do with how I'm using the property since in fact every property is displaying nothing (but I see it callback), so second would be about how to display the properties.
Well I am in the next step of the problem, but i have success in generating true form fields. In place of:
if self.instance:
self.fields['total_budgeted'] = self.instance.total_budgeted()
You can write:
if self.instance:
self.fields['total_budgeted'] = form.CharField(
initial=self.instance.total_budgeted(),
widget=HiddenInput()
)
In this code you I instantiate the form Field as CharField, you can use the FormField you want, and I hide it by choosing the Hidden input widget.

Django user injection in model properties

I have this models in Django:
News
Comments
Reactions
Relations are:
a News has various Comments
a Comment has various Reactions
The problem is the user (in request / session): the user may subscribe to a reaction, or a comment; he may be logged in or not. (it's a foo example, it doesn't have much sense)
I can't do in template:
{% for reaction in this_news.comments.reactions %}
{{ reaction.name }}
{% if reaction.user_subscribed %} #reaction.user_subscribed(request.user)...
You have subscribed this reaction!
{% endif %}
{% endfor %}
Problems are:
I can't call the method in the template with a parameter (see the comment above)
Models don't have access to request
Now i'm calling an init_user method in News Model, passing the request. Then i have the same method in Comment and Reaction model, and i have to set the user_subscribed property cycling the children of each model.
Isn't there a smarter way to do this?
EDIT: thanks to the Ignacio's hint about using custom tag i'm trying to do a generic mode to pass the user (avoiding the use of closures because i don't know how to use them atm):
def inject_user(parser, token):
try:
# split_contents() knows not to split quoted strings.
tag_name, method_injected, user = token.split_contents()
except ValueError:
raise template.TemplateSyntaxError("%r tag requires exactly three arguments" % token.contents.split()[0])
return InjectUserNode(method_injected, user)
class InjectUserNode(template.Node):
def __init__(self, method_injected, user):
self.method_injected = template.Variable(method_injected)
self.user = template.Variable(user)
def render(self, context):
try:
method_injected = self.method_injected.resolve(context)
user = self.user.resolve(context)
return method_injected(user)
except template.VariableDoesNotExist:
return ''
When i use it {% inject_user object.method_that_receives_a_user request.user %} i come to this error 'str' object is not callable in method_injected(user); how i can fix that?
Write custom template tags that take the user and set a context variable to indicate presence or absence of the criterion.
I've resolved it in a less elegant way, but it worked for me.
I've created a sort of singleton in my User defined class, with a property that i set in every view i need it.
The property is User.current.
Then, inside the models, where i need that i get the current user looking in User.current.

Populate a django form with data from database in view

I have a form in my forms.py that looks like this:
from django import forms
class ItemList(forms.Form):
item_list = forms.ChoiceField()
I need to populate the item_list with some data from the database. When generated in HTML item_list should be something like:
<select title="ItemList">
<option value="1">Select Item 1</option>
<option value="2">Select Item 2</option>
</select>
The options values in my select statement will change almost every time since a variable in the query will often change generating new results.
What do I need to put in the view.py and also in my template files to populate the ItemList with values from the database?
Take a look at this example in the Django documentation:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#a-full-example
Basically, you can use the queryset keyword argument on a Field object, to grab rows from your database:
class BookForm(forms.Form):
authors = forms.ModelMultipleChoiceField(queryset=Author.objects.all())
Update
If you need a dynamic model choice field, you can hand over your item id in the constructor of the form and adjust the queryset accordingly:
class ItemForm(forms.Form):
# here we use a dummy `queryset`, because ModelChoiceField
# requires some queryset
item_field = forms.ModelChoiceField(queryset=Item.objects.none())
def __init__(self, item_id):
super(ItemForm, self).__init__()
self.fields['item_field'].queryset = Item.objects.filter(id=item_id)
P.S. I haven't tested this code and I'm not sure about your exact setup, but I hope the main idea comes across.
Resources:
http://www.mail-archive.com/django-users#googlegroups.com/msg48058.html
http://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.ModelChoiceField
What you need to do is to find out which object do you actually want for e.g. if you want to find out a book named "Upvote-if-u-like!" then your urls.py should like
urlpatterns = [
path('textshare/<str:slug>',views.extract,name="textshare"),]
now when someone will search for mybook.com/textshare/upvote-if-u-like!/
it will take him/her to views.py which would look like
def extract(request,slug):
context={}
obj=bookForm.objects.get(title=slug)
form=bookModelForm(instance=obj)
context={'form':form}
return render(request,'bookfound.html',context)
where bookForm is in Models.py and bookModelForm is in forms.py
Happy Djangoing:)

Django: How to check if there are field errors from within custom widget definition?

I'd like to create widgets that add specific classes to element markup when the associated field has errors.
I'm having a hard time finding information on how to check whether a field has errors associated with it, from within widget definition code.
At the moment I have the following stub widget code (the final widget will use more complex markup).
from django import forms
from django.utils.safestring import mark_safe
class CustomTextWidget(forms.Widget):
def render(self, name, value, attrs):
field_has_errors=False # change to dynamically reflect field errors, somehow
if field_has_errors:
error_class_string="error"
else:
error_class_string=""
return mark_safe(
"<input type=\"text\" class=\"%s\" value=\"%s\" id=\"id_%s\" name=\"%s\">" % (error_class_string, value, name, name)
)
Can anyone shed light on a sensible way to populate the field_has_errors Boolean here? (or perhaps suggest a better way to accomplish what I'm trying to do). Thanks in advance.
As Jason says, the widget has no access to the field itself. I think a better solution though is to use the cascading nature of CSS.
{% for field in form %}
<div class="field{% if field.errors %} field_error{% endif %}">
{{ field }}
</div>
{% endfor %}
Now in your CSS you can do:
div.field_error input { color: red }
or whatever you need.
The widget has no knowledge of the field to which it is being applied. It is the field that maintains information about errors. You can check for error_messages in the init method of your form, and inject an error class to your widget accordingly:
class YourForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(YourForm, self).__init__(*args, **kwargs)
attrs = {}
if self.fields['your_field'].error_messages is not None:
attrs['class'] = 'errors'
self.fields['your_field'].widget = YourWidget(attrs=attrs)