1
If you are querying a unique entry, use .get
tweets = Tweet.objects.get(username__iexact=username)
instead of .filter.
The filter() method will return an array, even if it is only one result.
And also, your query is wrong… You have username on your model, not user
(username__iexact=username)
0
You need to filter your Tweets as such:
tweets = Tweet.objects.filter(username__iexact=username)
If you only want one Tweet back:
tweet = Tweet.objects.get(username__iexact=username)
Which tweet instance is returned would depend on the default ordering of the model, which is id
unless you specify it in the model’s Meta
options.
In the template, you need to either loop over the tweets returned to display tweet.username
which only exists on an instance, or you need to pass in the username and just display:
<h1>{{ username }}</h1>
- [Answer]-Using 'form' in 'login.html' I got this error message. 'url' requires a non-empty first argument
- [Answer]-AngularJS $http.post not getting JSON success response. Instead redirects to URL of the REST endpoint
- [Answer]-Django Optimisation of a queryset with Q objects
- [Answer]-Angular get json from server but cannot display columns only whole model
- [Answer]-Django/Python Chain and Sort Querysets
0
The Tweet model described above does not have any field user so the filter user__iexact will not work.
Assuming username is not unique as the model does not describe the username field to be unique
This can be changed as:
tweets = Tweet.objects.filter(username__iexact=username)
The filter queries always returns a list of objects.
TO access the first object use tweets[0]
to show the username we can use tweets[0].username
- [Answer]-Send two lists in my template, for' tag received an invalid argument: for i in liste ; j in liste2
- [Answer]-Resolve Static URL on Server