i have a django 'templating' question
if i have in views.py:
def cv(request):
if request.user.is_authenticated():
cv = OpenCv.objects.filter(created_by=request.user)
return render_to_response('cv/cv.html', {
'object_list': cv,
},
context_instance=RequestContext(request))
and in cv.html something like:
{% for object in object_list %}
<li>
First Name {{ object.first_name }} <br />
Last Name {{ object.last_name }} <br />
Url {{object.url}} <br />
Picture {{object.picture}} <br />
Bio {{object.bio}} <br />
Date of birth {{object.date_birth}} <br />
{% endfor %}
but i want this content to appear on the profile.html page too, how can i do it?
a smple {% include "cv/cv.html" %} in the profile.html doesn't work.
Also, is there another way to 'parse the object list' than explicitly write all the objects, like above?
thanks in advance!
You need to pass object_list to profile.html as well.
It's not very clear what you're asking, but I suspect a custom template tag might be what you're after.
Related
I have this code which takes the name into the "nm" variable
{% extends "base.html" %}
{% block title %}Search Page{% endblock %}
{% block content %}
<form action="#" method="post">
<p>User or Group:</p>
<p><input type="text" name="nm"/></p>
<p><input type="submit" value="submit"/></p>
</form>
{% endblock %}
I want to put this variable into a different html file which will show different things.
Can anyone suggest me the most efficient way to make this happen?
If you're using Django, send the nm text field back to your view in views.py, where you should access that variable (nm = request.POST.get("nm")) and then send that via the context variable in the render() function to your other HTML file (return render(request, "your_other_file.html", {"nm": nm})), where you can access the variable via interpolation ({{ nm }})
I am creating a web application using django, and I want now to add a view that modifies entries (my web application is an encyclopedia, so the view let us editing the page).
We may be able to access the editing page by cliking on the link of this html page:
{% extends "encyclopedia/layout.html" %}
{% block title %}
{{ title }}
{% endblock %}
{% block body %}
{{ html | safe }}
{% if exists %}
<br><br><br><br>
Edit encyclopedia
{% endif %}
{% endblock %}
So django'll go through this url
urlpatterns = [
...
...
...
path("<str:title>/edit", views.edit, name="edit"),
]
Then, this url should bring us to a this view:
def edit(request, title):
if request.method == "POST":
form = NewForm(request.POST)
if form.is_valid():
with open(f"entries/{title}.md", "w") as file:
file.write(form.cleaned_data["content"])
return redirect(reverse("encyclopedia:display"))
else:
return render(request, "encyclopedia/edit.html",{
'form' : NewForm(),
'message' : """<div class="alert alert-danger" role="alert">
Your entries are empty.
</div>"""
})
markup = util.get_entry(title)[0]
print(request.)
return render(request, "encyclopedia/edit.html",{
'form' : NewForm(),
'title' : title,
'markup': markup,
})
And here is my html file:
{% extends "encyclopedia/layout.html" %}
{% block title %}
Edit
{% endblock %}
{% block body %}
<h1>New Encyclopedia</h1>
<p>Our websites' encyclopedias are written in a langage names Markdow<br>
You may have additionnal informations about this language here.</p>
<form action="{% url 'encyclopedia:edit' %}" method="POST" style="display: block; text-align: center; padding: 20px;">
{% csrf_token %}
<p style="margin: 15px;">{{ title }}</p>
<textarea rows='10' placeholder="Encyclopedia's content" name="content" style="width: 90%; margin: 15px;">{{ markup }}</textarea>
<input type="submit" value="Edit" style="width:15%">
</form>
{% endblock %}
But my problem is that when I run my application, and go to the editing page, I get a NoReverseMatch error just like this one:
NoReverseMatch at /wiki/Django/edit
Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P[^/]+)/edit$']
I think that this problem is linked to the fact that I don't give the title argument when I call the editing view in my form, but I don't know how to do this.
If anyone could help me that would just be amazing, I made many researches, but couldn't really understand how to fix the problem...
I've finally found the origin of the error, to whom it may help, it was that I forgott to add the 'title' variable (that is obligatory for my view) just like this:
else:
return render(request, "encyclopedia/edit.html",{
'form' : NewForm(),
'message': """<div class="alert alert-danger" role="alert">
Your entries are empty.
</div>""",
'title' : title,
})
The is_valid() method doesn't really corresponds to how do I want to treat data, I've replaced it with this function:
undecodables = ["è", "à", "é"]
def own_validation(string):
try:
valid = False
for i in string:
if i.isalnum : valid = True
assert i not in undecodables
except AssertionError:
return False
else:
return valid
I don't know what are all the undecodable characters, so the list'll be completed in the future.
Thanks to #iklinac for your help !
why is it i cant put value from my database to input type date? even though my query is correct?
{% for feedback in feedbacks %}
<input name="datef" value="{{feedback.dateSubmitted}}" type="date">
{% endfor %}
this is my views.py
feedback = obj.objects.all()
print(feedback)
this is the result for print
<QuerySet [<TrEmployeeSuppliersFeedbackQuestionsSubmittedRecords: mystudent>]>
my models.py
class TrEmployeeSuppliersFeedbackQuestionsSubmittedRecords(models.Model):
.....
dateSubmitted = models.DateField(auto_now_add=True, null=True, blank=True)
.....
the result in my webview
UPDATE: i change my html to this , and this is the result in my webview
html
{% for feedback in feedbacks %}
{{feedback.dateSubmitted}} <input name="datef" value="{{feedback.dateSubmitted}}" type="date">
{% endfor %}
I suspect the formatting is off. The date input probably wants something like year-month-date. Try passing it like this:
<input name="datef" value="{{feedback.dateSubmitted|date:'Y-m-d'}}" type="date">
If the above doesn't work, also try:
<input name="datef" value="{{feedback.dateSubmitted|date:'d/m/Y'}}" type="date">
for all user inputs i think it will be better to use django forms or ModelForms
after that django will format all values for you and it will be more cleaner way to validate user data, and save it to database. for multiple forms you can use formset
in view you create formset:
TrEmployeeSuppliersFeedbackQuestionsSubmittedRecordsFormSet = modelformset_factory(
TrEmployeeSuppliersFeedbackQuestionsSubmittedRecords,
fields=('dateSubmitted ', )
)
formset = TrEmployeeSuppliersFeedbackQuestionsSubmittedRecordsFormSet()
and add formset as parameter to your html template
and then in html template you can
{{ formset.management_form }}
{% for form in formset %}
{{ form.dateSubmitted.value }} {{ form.dateSubmitted }}
{% endfor %}
In a loop I iterate over several URLs stored in movie.poster. If I only print movie.poster it returns the URL.
<ul>
{% for movie in all_movies %}
<li> {{ <img src="movie.poster" alt="Cheetah!" /> }} </li>
<!--<input type = "submit" value = "delete"/>-->
<!-- <img src="movie.poster" alt="Cheetah!" />-->
{% endfor %}
</ul>
When I run the code below I get the following error message "Could not parse the remainder: […]" How could I fix this?
use {{ }} for variables not for a line, and .url to call an image
so:
<li><img src="{{movie.poster.url}}" alt="Cheetah!"></li>
will call the image
I need to get some data using a dynamic name from front matter:
{{ site.data.prd-[page.tag].title" }}
The above fails to get the string from the data folder and nothing is output.
Where am I going wrong?
I am not sure about what you are trying to do but how about make a dictionary type data in _data directory and search for a title that matches page.tag in desired pages?
_data.prd-tags.yml
For each item, we can access the key and value using index ([0]: key, [1]: value).
jekyll: "jekyll's title"
ruby: "ruby's title"
html: "html's title"
.
.
.
HTML fragment for searching and displaying a tag (if found)
Iterate over site.data.prd-tags and display title if an item that matches page.tag is found.
<div class="tag-title">
{% for tag in site.data.prd-tags %}
{% if tag[0] == page.tag %}
{{ tag[1] }}
{% else %}
Nothing found
{% endif %}
{% endfor %}
</div>