Django: How to get user full name from user id - django

My model:
author=models.ForeignKey(User)
User id (ex. 174948) is saved to column author_id.
In the template I show user id with {{post.author}}
I want also to show full name for that particular user.
How to do that in the template?

By using the method that does it.
{{post.author.get_full_name}}

It depends if the user object has the first and/or last name fields set.
If not (for example a new user was just created), then calling {{ post.author.get_full_name }} may return an empty string.
This method is more reliable:
{{ post.author.get_full_name|default:post.author.username }}

Another way
{{post.author.first_name}} {{post.author.last_name}}

Related

Django: Generate form from dictionary

I'm currently working on a django-app where users have the option to select synonyms for a select number of words. These words are then replaced all over the website by these synonyms
these synonymes are defined as a separate model:
class TransportSynonyms(models.Model):
user= models.ForeignKey(user)
key = models.CharField(max_length=255)
value = models.CharField(max_length=255)
This process is done in part with template tags, and as such the number of words that can be 'synonymed' is limited. For example, the following words can be replaced by synonymes:
'train', 'plane'.
And appear like so in the html template:
{% get_trans "train" %}
{% get_trans "plane" %}
I now want to give user the ability to define synonymes for themselves, without the admin view. I created a few pages using a ListView as an overview (so the users could see which words they can change) with individual buttons that led to EditViews.
However, I'm since user have no synonyms linked to them by default, the ListView appears empty. I could solve that by passing a list from the view, but that list wont have the required '.id' values and would be worthless for linking to the EditViews.
My listview currently looks like the following table:
Original | Synonym
---------------------------
train | train (button to editview)
plane | aircraft (button to editview)
The buttons require an ID value
href="{% url 'synonym_edit' pk=synonym.id %}"
I have to find a way to fill the ListView with the 'synonymable' words and provide links to a DetailView (for synonyms might not yet exist).
I thought about dropping the ListView all together and instead pass a dictionary to a form. By default both the key and the value will be the same word
({'train' : 'train', 'plane' : 'plane'})
and a form would then be generated form this dictionary, allowing users to change the value by having the dictionary value be shown as a text input.
Its a bit more limited (and more cumbersome) than my original plan, but I think this might work. Problem is that I've only really worked with modelforms before, and I am currently stuck on having the form be generated from the dictionary. Could anyone here point me to the right direction?
Regards,
Jasper

Django: How to make this GET request with a hyperlink?

So if I want a user's name to link to it's profile, I want the link to contain the user's first name and last name in GET form...Do I just hard-code this into the link, or is there a more elegant way of coding this? Here is hard-coded:
<p>
<li role="presentation" class="active">{{ user.fname }}
</li>
</p>
Yes, the more elegant way would be to set up a route that takes, for example, the user's ID (pk) and use either a DetailView set up for the User model or a function view that accepts the ID and retrieves the relevant user. Passing in the first and last name directly means you need to query on both of them (after pulling them out of the querystring) rather than simply querying on the ID as it's passed to you (after the framework politely checks its type, provided you've set up the route correctly).
So your route would look something like url(r'^profile/(?P<pk>\d+)/$', ProfileView.as_view(), name='profile') and your template tag would look something like {% url 'profile' user.id %}.

Passing the complete Object Instance, NOT just the object id, from a Django template to view

I am trying to pass full object instances from my django template to my view as the values associated with checkbox inputs. When I try to do so, I only get the object's unicode method representation and am thus unable to access any of the object's particular fields in my view.
Is there either a way to pass the actual object from the template to the view or, instead, a way to use that unicode representation in the view to access the actual object and its corresponding fields?
I don't want to use 'publication.id' as my checkbox value because I can't guarantee the ids will never change.
Current code:
mytemplate.html
<form method="get" action="processData">
{% for publication in mydata %}
<input type="checkbox" name="item-selection" value="{{publication}}">
{% endfor % }
view.py
items = request.GET.getlist('item-selection')
for x in items:
print x # this prints the unicode mehtod's return value
x.datafield1 # I want to access each object's data
No, this can't possibly work: sending data from the browser to the view is done via HTTP post, and you can only post data, you can't post objects.
But you've said the solution yourself: use the IDs. Who cares if the IDs change? All that matters is that you get the ID as it currently is.
<input type="checkbox" name="item-selection" value="{{publication.id}}"><label>{{ publication.name }}</label>
...
item_ids = request.GET.getlist('item-selection')
items = Item.objects.filter(id__in=item_ids)

Django if statement with foreign key

Cut it short I have a model called Page and a field called "parent" that links to itself, I want to write if the nav.parent has a parent called home then do this but for some reason it doesn't work.
{% if nav.parent == "home" %}
The problem is of course that Django doesn't know which field to use when comparing, unless you tell it. Since "home" is in the title field, you need to actually specify that field:
{% if nav.parent.title == "home" %}
You can't do this in the template.
You have 2 options as a solution to your problem:
The bad one: pass the variables in the context from the view.
The best: create a custom tag which will receive nav as a parameter and find its parent:
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/

Dynamically set default form values in django

I'm trying to find a way to dynamically set default form values. So for example, if I add a facebook login feature on my webpage, and I can get his first name and last name from what the facebook javascript returns, I want to be able to set these values as the new "value" parameter in the given form. (basically so I'm able to put the user in my database)
I was hoping that there is some sort of
{{ form.firstname.default = Javascript.return.firstname }}
that I can insert into a template but there isn't...
Any ideas? Thank you.
Edit;; Maybe it would be better to first pass in the information into a views.py? But how would I do this? I am just considering hand writing the inputs out in the javascript field, which would be annoying...
You can set the default value of a template variable:
{{ form.firstname|default:Javascript.return.firstname }}
assuming that {{ Javascript.return.firstname }} is valid in that template context.
The code generated by Django is all server-side, its templates can't use anything which isn't available when the page is loaded. You need to use javascript to set the value.
For example, if your form looks like this:
<form id="login">
<input name="firstname" />
</form>
You could use this javascript (assuming you're using jQuery):
// Do the facebook login, set the var fname to the first name, then:
$('#login [name=firstname]').val(fname);