Django Shows Page Source Instead Of My Webpage - python

I have found that when passing context and a title into my views it causes the page to only show its html source code instead of the actual frontend graphics. If i remove the title being passed in then it works again but i do want to have the title passed in so is there any way to have this work?
Views
def myPosts(request):
context = {
'products': Post.objects.all()
}
return render(request, 'create/my_posts.html', context, {'title_page': 'My Posts'})
class PostListView(ListView):
model = Post
template_name = 'my_posts.html'
context_object_name = 'products'

when you are passing context yo can do something like this
context = {
'products': Post.objects.all(),
'title_page': 'My Posts'
}
and finally pass
return render(request, 'create/my_posts.html', context)
In the front end, you can use it as context.title_page

Related

Django views and template tags

I have an app that request an input from a user and after the validation it's querying the database for a match, i did it in my html using if and for loop to match the string, but now i did a function to generate a pdf in my views.
The question is how can i access my results from html and get it in my pdf instead of query again to match the results?
def getting_games(request):
form = GamesForm(request.POST or None)
queryset = Games.objects.all()
if form.is_valid():
searched = request.POST['game_number']
queryset = Games.objects.filter(game_number__iexact=searched).values()
ctx = {'form': form,
'queryset': queryset }
return render(request, 'results.html', ctx)
context = {
'form': form
}
return render(request, "base.html", context)

using python markdown .convert() in django views

I have a blog that uses markdown. I am trying to convert it to HTML in views.py with python markdown. For individual posts, this code works:
def detail_view(request, slug):
md = markdown.Markdown()
post = get_object_or_404(Post, slug=slug)
postcontent = md.convert(post.md_text)
context = {...}
return render(request, 'details.html', context)
However, applying the same logic to a view meant to show multiple posts fails:
def home_view(request):
posts = Post.objects.order_by('-id')
md = markdown.Markdown()
for markdown in posts:
postcontent = md.convert(posts.md_text)
context = {...}
return render(request, 'home.html', context)
What would I need to change to convert the text_md field(s) of my Post model in this view? Is .convert() the right method?
Thanks in advance!

Wtform FieldList rendering in Django Template

I have declared 2 form like this.
class TripItemForm(Form):
.....
class ShiftMainform(Form):
trip_list = FieldList(FormField(TripItemForm),validators=[checking_time])
Now in django view class I have initialize this trip_list field like this
class DjangoView(View):
def get(self, request, id):
form = ShiftMainform()
form.trip_list = list_trips #here list_trips is the list of TripItemForm objects.
context = {
'form': form,
}
return render(request, 'example.html', context)
When the html file rendered it shows that form.trip_list is empty. Can anyone tell me why is this happen? What I've done wrong or what should be the right way to render FieldList field in django tempalte.
Thanks in advance.

The view blog.views.BlogViews didn't return an HttpResponse object. It returned None instead

I'm having a problem with the views for my blog main page. I've read a lot of problems with the same error but couldn't find a solution that i thought matched with my problem.
I am trying to emulate my old code with a new View so that i can customise it further in the future.
path('', ListView.as_view(
queryset=Post.objects.filter(posted_date__lte=now).order_by("-posted_date")[:25],
template_name="blog/blog.html")),
i was previously using just the above url pattern to display my blog posts but im moving to a view:
def BlogViews(TemplateView):
def get(self, request):
posts = Post.objects.all().order_by("-posted_date")[:25]
args = {
'posts' : posts,
}
return render(request, 'blog/blog.html', args)
and the new url
path('', BlogViews, name='blog'),
However this view is not working and returns the error in the title.
I'm importing TemplateView, Post and render correctly i believe.
TemplateView is a class-based-view, so you subclass it with class.
class BlogViews(TemplateView):
def get(self, request):
posts = Post.objects.all().order_by("-posted_date")[:25]
args = {
'posts' : posts,
}
return render(request, 'blog/blog.html', args)
Then use .as_view() in the URL pattern.
path('', BlogViews.as_view(), name='blog'),
Avoid overriding get or post for class-based views. You end up losing the functionality of the parent class or duplicating it. You can take your ListView.as_view(...) as the starting point for the view, for example:
class BlogView(ListView):
template_name="blog/blog.html"
def get_queryset(self):
# Use get_queryset instead of queryset because you want timezone.now()
# to be called when the view runs, not when the server starts
return Post.objects.filter(posted_date__lte=timezone.now()).order_by("-posted_date")[:25]
In your case, it might be easier to use a function-based-view:
def blog_view(request):
posts = Post.objects.all().order_by("-posted_date")[:25]
args = {
'posts' : posts,
}
return render(request, 'blog/blog.html', args)
Then include it in the URLs with:
path('', blog_view, name='blog'),

Django: Passing string parameters to class-based view not working

I have tried following this suggestion to pass string parameters to a class based view but it does not seem to work.
the url:
url(r'^chart/(?P<chart_name>\w+)/$',
ChartView.as_view(chart_name='chart_name'), name="chart_url"),
the view:
class ChartView(View):
template_name = "chart.html"
chart_name = None
def post(self, request, *args, **kwargs):
form = DatesForm(request.POST)
context = {
'form': form
}
return render(request, self.template_name, context)
def get(self, request, *args, **kwargs):
print("test")
form = DatesForm()
# fetch plot data (default values used)
context = {
'form': form,
'chart_name': self.chart_name
}
return render(request, self.template_name, context)
the link that is supposed to be redirecting to the view:
Sometext
(namespace 'chartboard' given in the project's urlconf).
the error:
NoReverseMatch at /chart/lords/
Reverse for 'chart_url' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['chart/(?P<chart_name>\\w+)/$']
For what its worth, "test" gets printed twice to the console output (why?)
Using django 1.8.11 and python 3.4.3 on Ubuntu 14.04.04
You should access the chart_name using kwargs:
# urls.py
url(r'^chart/(?P<chart_name>\w+)/$',
ChartView.as_view(), name="chart_url"),
# and then in the view
class ChartView(View):
template_name = "chart.html"
def get(self, request, *args, **kwargs):
form = DatesForm()
context = {
'form': form,
'chart_name': kwargs['chart_name'] # here you access the chart_name
}
return render(request, self.template_name, context)
The post you have considered for implementing this is for making sure that a variable is available in templates and that is taken care of by setting it up in context which is passed to the template render.
The problem you are facing here is to access a named group defined in the url pattern.
Here is more documentation on how django process a request when you try to access a URL.

Categories

Resources