how to get the data from view to templates? - html

i'm trying to get the data from my views.py to the html page.
if the views.py code is this
def VerifiedBySuperuser(request):
if request.method == 'POST':
vbs = MaanyaIT_EXAM48_ManageQuestionBank()
vbs.QuestionID = MaanyaIT_EXAM48_ManageQuestionBank.objects.get(QuestionID=request.POST.get(QuestionID, None))
vbs.QuestionInEnglishLang = request.POST.get('QuestionInEnglishLang', None)
vbs.save()
else:
return render(request, 'exam48app/verifiedbysuperuser.html')
then what shoud the code of html page to view my all data to the tamplates..
this is my html page
<form class="from-horizontal" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="post-entry">
{{ MaanyaIT_EXAM48_ManageQuestionBank.QuestionInEnglishLang }}
</div>
</form>
now what should i do?

From your comment, you need to know How to write/render the data from view to html template
I will demonstrate a simple example for you,
Assuming you have a view as below,
def VerifiedBySuperuser(request):
if request.method == 'GET':
context = {
"T_Name": "My Name",
"T_Age": 50,
"T_Phone": 1478523699
}
return render(request, 'verifiedbysuperuser.html', context=context)
and a HTML template as follows,
<!DOCTYPE>
<html>
<body>
Name : {{ T_Name }}<br>
Age : {{ T_Age }}<br>
Phone : {{ T_Phone }}<br>
</body>
</html>
When you access your view, you will get a response like this,
In your case, you can pass as many attributes to template as dict (shown in my example) and in template/html keys of context (that is T_Name,T_Name etct) becomes variable. So you can directly use them in HTML inside the twin braces ({{ variable_name }})
As far as I knew, this is the general procedure for template rendering/ html rendering
UPDATE-1
def VerifiedBySuperuser(request):
if request.method == 'POST':
obj = MyModel.objects.get(id=some_id)
other_data = [1,2,3,4,] # some specific data
context = {
"post_data": request.data,
"object_instance": obj,
"some_other_data": other_data
}
return render(request, 'verifiedbysuperuser.html', context=context)

Related

While trying to reverse a url in django template html file, exception 'NoReverseMatch' occurs. I included the additional parameter in views function

def entry(request, name):
content = util.get_entry(name.strip())
if content == None:
content = "## Page was not found"
content = markdown(content)
return render(request, "encyclopedia/entry.html", {'content': content, 'title': name})
def edit(request,title):
content = util.get_entry(title.strip())
if content == None:
return render(request, "encyclopedia/edit.html", {'error': "404 Not Found"})
if request.method == "POST":
content = request.POST.get("content").strip()
if content == "":
return render(request, "encyclopedia/edit.html", {"message": "Can't save with empty field.", "title": title, "content": content})
util.save_entry(title, content)
return redirect("entry", name=title)
return render(request, "encyclopedia/edit.html", {'content': content, 'title': title})
util has files that help get names of entered files, save a new entry or get content of entry.
{% extends 'encyclopedia/layout.html' %}
{% block title %}
{{title}}
{% endblock %}
{% block body %}
Edit This Page
{{entry | safe }}
{% endblock %}
layout has the standard block code of HTML
edit.html contains HTML code that gives a button on each page so that we can edit the content of the page entry and passes name='content' for the content to be edited.
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:name>", views.entry, name = 'entry'),
path("search", views.search, name= 'search'),
path("newpage",views.create,name='create'),
path("random",views.random,name='random'),
path("edit", views.edit, name='edit')
]
THIS IS A CS50w project and I have taken references from other sources.
You are passing variable to the url while it doesn't really need any
Just add title parameter to edit path
path("edit/<str:title>/", views.edit, name='edit')
when passing an argument to django view, you have to set it in the url
<type:argument_name>
and in the view arguments
def viewname(request, variable1, variable2...): pass

Image is not editing after click on update button

I am going through a Django tutorial and My blog post image is not editing for edit post in my blog app. I use Django==3.1.2.
views.py
def edit_post(request, post_id):
post = BlogPost.objects.get(id=post_id)
if request.method != 'POST':
form = UpdatePost(request.POST or None, request.FILES or None, instance=post)
else:
form = UpdatePost(instance=post, data=request.POST)
if form.is_valid():
form.save()
return redirect('mains:posts')
context = {'post':post,'form':form}
return render(request, 'mains/edit_post.html', context)
forms.py
class UpdatePost(forms.ModelForm):
class Meta:
model = BlogPost
fields = ['blog_post_title','blog_description','image']
edit_post.html
{% block content %}
<div class="container">
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<button type="submit">Save Changes</button>
</form>
</div>
{% endblock content %}
The Problem
I am trying to edit my post in my blog app, Everything is working fine ( Blog title and Blog description is changing ) - when i change image it returns ( redirect ) fine BUT image not changing.
I am stuck with this problem and have no idea what is wrong.
What have i tried.
1). When i first create the view then i didn't add (request.POST or None, request.FILES or None), BUT
when i notice that, this may be effecting from editing then i added and it still not editing the image.
2). I have also changed template before BUT nothing works.
3). I have also changed form before BUT nothing workes.
Help me in this. I will really appreciate your Help. Thank you in advance !!
try this...
def edit_post(request, post_id):
post = BlogPost.objects.get(id=post_id)
if request.method != 'POST':
form = UpdatePost(request.POST or None, request.FILES or None, instance=post)
else:
form = UpdatePost(instance=post, data=request.POST, files=request.FILES)
if form.is_valid():
form.save()
return redirect('mains:posts')
context = {'post':post,'form':form}
return render(request, 'mains/edit_post.html', context)

how to pass value from html to view in django?

I have made this HTML code:
<h3>your major is {{user.userprofile.major}}</h3>
This will correctly show the major on the webpage, but I want to use this string to get something from another table in view.
How would I pass this string to view?
edit:
Here is my view.py
def dashboardView(request):
obj = BooksFile.objects.all()
query = BooksFile.objects.filter(book_major='cs)
return render(request, 'dashboard.html', {'books': obj, 'major': query})
def registerView(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
profile_form = UserProfileForm(request.POST)
if form.is_valid() and profile_form.is_valid():
user = form.save()
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
return redirect('login_url')
else:
form = UserCreationForm()
profile_form = UserProfileForm()
context = {'form': form, 'profile_form': profile_form}
return render(request, 'registration/register.html', context)
here is my template:
{% extends 'index.html' %}
{% block content %}
<h1>Welcome, {{user.username}}</h1>
<h2>Your major is {{user.userprofile.major}}</h2>
{% for book in books %}
<h3>Your book name is {{book.book_name}}</h3>
{% endfor %}
{% endblock %}
I am trying to show the book names from the booksfile table by corresponding major that user has. Right its showing the books that has "cs" attribute because I manually put "cs" in the get function in view. I am trying to send the major string from template to view, so that I can put what ever the user's major is in the get function. Or is there any other way to do it.
You need to use a form in your template and submit it to call your view. i.e.
<form action="your_view_url" method="POST">
<input type="text" name="major" value="{{user.userprofile.major}}"/>
<input type="submit"/>
</form>
an then in your view you access that with:
if request.POST:
major = request.POST.get('major')
As per documentation: https://docs.djangoproject.com/en/2.2/topics/forms/
First of all you have to get the value of model with help of queryset, and put it in the dictionary and then pass it with the template.
In views:
def get(self, request):
queryset = Model_name.objects.all()
ctx = {
'queryset': queryset,
}
return render(request, 'page_name(or template_name).html', ctx)
in template:
<form action="{%url'(your_view_name without brackets)'%}" method="POST">
{% for data in queryset%}
<span class="username">{{data.name(field of your model)}} .
</span>
<span class="email">{{data.email(field of your model)}} .
</span>
{% endfor%}
</form>

Django how to adding comments option on a post

I am developing a blog which i want to add comment form option to it, i have added the form to the same page directly under the article, i want that went a user comment it should redirect to the same page with the article but i keep getting and error
here is my code
view
def comment(request, article_id):
try:
article = Article.objects.get(pk=article_id)
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.cleaned_data['comment']
article.comments_set.create(comment=comment)
#messages.infos(request,comment)
return redirect('blog:_article')
#else:
#pass
#form = CommentForm()
#context['form'] = form
#return render(request,'blog/comment.html', context)
except Exception as e:
#wriet error to file
return render(request,'blog/404.html')
urls
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.index, name='index'),
path('<int:article_id>/article', views._article, name='_article'),
path('<int:article_id>/comment', views.comment, name='comment'),
]
models
class Comments(models.Model):
comment = models.TextField()
date = models.DateTimeField(default=timezone.now)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
def __str__(self):
return self.comment
form
<form method="post" action="{% url 'blog:comment' article.id %}">
{% csrf_token %}
{% for field in form %}
{{ field.label_tag }}
{% render_field field class="form-control is-valid" rows="4" %}
{% endfor %}<br>
<button class="btn btn-success">Post</button>
</form>
I finally did it by adding the code to handle the comment in the same view that renders the articles this is my code
def _article(request, article_id):
try:
article = Article.objects.get(pk=article_id)
related_articles = Article.objects.filter(tags=article.tags).exclude(pk=article.pk)[:4]
context['article'] = article
context['related_articles'] = related_articles
context['form'] = CommentForm()
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.cleaned_data['comment']
article.comments_set.create(comment=comment)
return render(request,'blog/article.html', context)
except Exception as e:
#write error to file
return render(request,'blog/404.html')
If you don't want the page to redirect to another page or update the page, you should use AJAX (https://www.w3schools.com/jquery/jquery_ajax_get_post.asp) in this case. Your form will hit the url in the action by changing your page to that url so you have to handle the redirecting and rendering in your commenting view to come back to same page if you don't wanna do this dynamically.

Flask validate and upload file to web page to

I've been attempting to upload and use a file to my web page but continually run into errors. I have searched repeatedly and tried many different solutions with no success. Any direction would be appreciated.
/app/forms.py
class UploadForm(FlaskForm):
cadfile = FileField('cadfile', validators=[FileRequired(), FileAllowed(['STEP','STP'], 'Step Files Only')])
upload = SubmitField('Upload')
def __init__(self, *args, **kwargs):
super(UploadForm, self).__init__(*args, **kwargs)
self.user = None
def validate(self):
initial_validation = super(UploadForm, self).validate()
if not initial_validation:
print('Fails')
return False
return True
This part of the code fails everytime at "not initial_validation", regardless of no file, incorrect file type or correct file type.
/app/routes.py
#app.route('/index', methods=['GET', 'POST'])
def index():
form = UploadForm()
if form.validate_on_submit():
if 'cadfile' in request.files:
cadfile = request.files['cadfile']
cadfile.save('/app/tmp/' + cadfile.filename)
print('Success')
else:
print('No Go')
return redirect(url_for('details'))
return render_template('index.html', title='Home', form=form)
And my html file
/app/templates/index.html
<html>
<head>
<title>Upload</title>
</head>
<body>
<h1>Let's Do This</h1>
<form method="post" enctype="multipart/form-data">
{{ form.hidden_tag() }}
{{ form.cadfile }}
{{ form.upload() }}
</form>
</body>
</html>
I would like to have it print out 'No File' when no file is selected, 'Invalid File Type' when not a STP or STEP file, and 'Success' when file is successfully uploaded.
Got it working. Here is my final code
/app/forms.py
class UploadForm(FlaskForm):
print('called uploadform')
cadfile = FileField('cadfile', validators=[FileRequired()])
upload = SubmitField('Upload')
/app/routes.py
def index():
form = UploadForm()
FILE_TYPES = set(['STEP'])
var.company_id = current_user.company_id
if form.validate_on_submit():
print('Validated')
submit_name = form.cadfile.data.filename
if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
print('Yea Baby it is!')
...
code
...
return redirect(url_for('details'))
else:
form.cadfile.errors.append('File is not an accepted format')
return render_template('index.html', title='Home', form=form)
/app/templates/index.html
<form method="post" enctype="multipart/form-data">
{{ form.hidden_tag() }}
<p>
{{ form.cadfile }}
{{ form.upload() }}<br>
{% for error in form.cadfile.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
</form>
</body>
Thanks