I am creating an edit option for my entry. But instead of redirecting to the entry page, it is taking me to the addpage to create a new form. How do I redirect to the edit page?
Also, how do I make sure that the users previous input would reflect when they click on the edit button. I used initials - is this the best way to do it since I'm not using models but rather forms.Form.
VIEWS.PY
class AddPageForm(forms.Form):
title = forms.CharField(max_length=20)
content = forms.CharField(widget=forms.Textarea(
attrs={
"class": "form-control",
"placeholder": "Tell us more!"
})
def edit_page(request, title):
entry = util.get_entry(title)
if request.method == "POST":
form = AddPageForm(request.POST, initial={
"title": title,
"content": content
})
if form.is_valid():
util.save_entry(title, content)
return redirect('encyclopedia:entrypage', title=title)
else:
form = AddPageForm()
return render(request, "encyclopedia/editpage.html", {"form":form})
EDIT PAGE
{% block body %}
<h1>Edit {{ title }}</h1>
<form action="" method="post">
{% csrf_token %}
{% form %}
<input type="submit" value="Submit" class="btn btn-secondary">
</form>
ENTRY PAGE
{% block body %}
{{ content|safe }}
Update
<!-- <input="text" name="title" value="{{game.title}}" />
<input="text" name="genre" value="{{game.genre}}" /> -->
{% endblock %}
URLS.PY
app_name = "encyclopedia"
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:title>", views.entry_page, name="entrypage"),
path("search", views.search, name="search"),
path("add_page", views.add_page, name="addpage"),
path("edit_page/<str:title>", views.edit_page, name="editpage")
]
I think the issue with your code is that it is calling a redirect on a url that doesn't exist. 'encyclopedia:entrypage' does not exist in your urls.py. There are two ways of fixing this. Option #1 Create a url for entry page in urls.py with name=entrypage and a parameter in the url denoting which entrypage. Or option #2 Return directly to the entrypage view in the edit_page function. Here's a rough sketch of how you would do that
In edit_page in views.py:
# ...Code above
if form.is_valid():
util.save_entry(title, content)
return entry_page_view(request, title)
# 'title' if this view has a title passed as a kwarg or arg
# Code below...
Related
I edited my views.py but I am having a bug, each time I edit the previous entry, it creates a new entry with what i edited instead of editing that copy. I've tried to retouch the possible errors. please help
VIEWS.PY
class AddPageForm(forms.Form):
title = forms.CharField(max_length=20)
content = forms.CharField(widget=forms.Textarea(
attrs={
"class": "form-control",
"placeholder": "Tell us more!"
})
def edit_page(request, title):
if request.method == "GET":
content = util.get_entry(title)
form = AddPageForm(initial={"content": title})
return render(request, "encyclopedia/editpage.html",
{"form": form})
else:
form = AddPageForm(request.POST)
if form.is_valid():
content = form.cleaned_data['content']
util.save_entry(title, content)
return redirect('encyclopedia:entrypage', title)
return render(request, 'encyclopedia/editpage.html', {'form': form})
EDIT PAGE
{% block body %}
<h1>Edit {{ title }}</h1>
<form action="" method="post">
{% csrf_token %}
{% form %}
<input type="submit" value="Submit" class="btn btn-secondary">
</form>
ENTRY PAGE
{% block body %}
{{ content|safe }}
Update
{% endblock %}
URLS.PY
app_name = "encyclopedia"
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:title>", views.entry_page, name="entrypage"),
path("search", views.search, name="search"),
path("add_page", views.add_page, name="addpage"),
path("edit_page/<str:title>", views.edit_page, name="editpage")
]
UTILS.PY
def save_entry(title, content):
"""
Saves an encyclopedia entry, given its title and Markdown
content. If an existing entry with the same title already exists,
it is replaced.
"""
filename = f"entries/{title}.md"
if default_storage.exists(filename):
default_storage.delete(filename)
default_storage.save(filename, ContentFile(content))
def get_entry(title):
"""
Retrieves an encyclopedia entry by its title. If no such
entry exists, the function returns None.
"""
try:
f = default_storage.open(f"entries/{title}.md")
return f.read().decode("utf-8")
except FileNotFoundError:
return None
I am using forms.Form because this is an assignment and the requirement states that we should use that instead of forms.Model
I am trying to make the existing content to be the initial value of the textarea (i.e what the user typed before while adding that page should show when the user wants to edit the page.) when the user clicks on the Edit button.
When I click on edit, the content previously written tends to go to the title page in the url. Can you please help show me why?
VIEWS.PY
class AddPageForm(forms.Form):
title = forms.CharField(max_length=20)
content = forms.CharField(widget=forms.Textarea(
attrs={
"class": "form-control",
})
)
class EditPageForm(forms.Form):
content = forms.CharField(widget=forms.Textarea(
attrs={
"class": "form-control",
})
)
def edit_page(request, title):
entry = util.get_entry(title)
if request.method == "GET":
form = EditPageForm(request.POST, initial={
"content": entry
})
else:
form = EditPageForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
content = form.cleaned_data['content']
util.save_entry(title, content)
return redirect('encyclopedia:entrypage', title)
return render(request, 'encyclopedia/editpage.html', {'form': form})
EDIT PAGE
{% block body %}
<h1>Edit {{ title }}</h1>
<form action="" method="post">
{% csrf_token %}
{% form %}
<input type="submit" value="Submit" class="btn btn-secondary">
</form>
ENTRY PAGE
{% block body %}
{{ content|safe }}
Edit
{% endblock %}
URLS.PY
app_name = "encyclopedia"
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:title>", views.entry_page, name="entrypage"),
path("search", views.search, name="search"),
path("add_page", views.add_page, name="addpage"),
path("edit_page/<str:title>", views.edit_page, name="editpage")
]
what's in get entry function in utils?
do something like this :-
entry = Model.objects.all().last()
form = EditPageForm(request.POST, initial={"content": entry.content}
I have been currently working on an easy Django website.
An error came up when I tried to implement a function that users can add comments to blogs. I succeeded to show the form where users can write their comments, but even if I pressed the add button, didn't save properly.
I don't even know where I made a mistake coz I knew how to post and save information to the database.
so, I ask you to help!!
# add_comment.html
{% extends 'base.html' %}
{% block title %}|コメント追加{% endblock %}
{% block content %}
<div class="header-bar">
← 戻る
</div>
<div class="body-container blog">
<div class="body-header">
<h1>コメント追加</h1>
</div>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button class='button' type="submit">追加</button>
</form>
</div>
</div>
{% endblock %}
# views.py
#login_required
def add_comment(request, pk):
blog = get_object_or_404(Blog, pk=pk)
if request.method == 'post':
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.blog = blog
comment.save()
return redirect('blog', pk=blog.pk)
else:
form = CommentForm()
context = {
'form': form,
}
return render(request, 'blog/add_comment.html', context)
# urls.py
from django.urls import path
from . import views
from .views import BlogCreate, BlogUpdate, BlogDelete, BlogList
# blogs
urlpatterns = [
path('', views.index, name='latest_blogs'),
path('my_blogs/', views.my_blogs, name='my_blogs'),
path('my_blogs/my_blog_search', views.my_blogs_search, name='my_blogs_search'),
path('drafts/', views.drafts, name='drafts'),
path('blog/<int:pk>/', views.blog, name='blog'),
path('blog/<int:pk>/comment/', views.add_comment, name='add_comment'),
path('create/', BlogCreate.as_view(), name='blog-create'),
path('update/<int:pk>/', BlogUpdate.as_view(), name='blog-update'),
path('delete/<int:pk>/', BlogDelete.as_view(), name='blog-delete'),
path('all_blogs/', BlogList.as_view(), name='all_blogs'),
path('all_blogs/all_blogs_search/', views.all_blogs_search, name='all_blogs_search'),
]
Any comments help me and thanks for your time in advance!!
I have these two functions, one of them (first one) adds a new entry and the second one edits the entry:
def add_entry(request):
if request.method == 'POST':
form = AddForm(request.POST)
if form.is_valid():
title = form.cleaned_data["title"]
content = form.cleaned_data["content"]
if util.get_entry(title) is None:
util.save_entry(title, content)
return redirect('entry', title)
else:
return render(request, "encyclopedia/add_entry.html", {
"form": AddForm(),
"title": title
})
return render(request, "encyclopedia/add_entry.html", {
"form": AddForm()
})
def edit_entry(request, title):
content = util.get_entry(title)
if request.method == 'POST':
form = AddForm(request.POST)
if form.is_valid():
title = form.cleaned_data["title"]
content = form.cleaned_data["content"]
util.save_entry(title, content)
return redirect('entry', title)
return render(request, "encyclopedia/edit_entry.html", {
"title": title,
"content": content
Here is my edit_entry.html page:
{% extends "encyclopedia/layout.html" %}
{% block title %}
Edit page
{% endblock %}
{% block body %}
<form action="{% url 'edit_entry' title %}" method="POST">
{% csrf_token %}
<h5>Title</h5>
<input type="text" value="{{ title }}">
<h5>Content</h5>
<textarea cols="30" rows="10">{{ content }}</textarea>
<input type="submit" value="Save Editing">
</form>
{% endblock %}
This is add_entry.html template
{% extends "encyclopedia/layout.html" %}
{% block title %}
Add new entry
{% endblock %}
{% block body %}
<h1>Create a new page</h1>
{% if title %}
<h6 style="color: red;">"{{title}}" page is already exists. Please, enter a different title</h6>
{% endif %}
<form action="{% url 'add_entry' %}" method="POST">
{% csrf_token %}
{{ form }}
<input type="submit" value="Create">
</form>
{% endblock %}
And here is my urls.py:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:title>", views.entry, name="entry"),
path("search", views.search, name="search"),
path("add_entry", views.add_entry, name="add_entry"),
path("wiki/<str:title>/edit_entry", views.edit_entry, name="edit_entry")
]
My entry view:
def entry(request, title):
if title not in util.list_entries():
return render(request, "encyclopedia/error.html", {
"error": "Page Not Found",
"query": title
})
else:
return render(request, "encyclopedia/entry.html", {
"entry": markdown2.markdown(util.get_entry(title)),
"title": title
})
The issue here when I click to save the content of the page doesn't change, I want to save the edits and display it with new content. Instead, it returns an old form with the old content (like doesn't change).
EDIT: based on your comments, I think it is better to start over.
Since you are doing some simple create and update, it maybe better to use generic views. Here is an example.
1.First and formost, you need a model.
in models.py,
from django.db import models
class Entry(models.Model):
title = models.CharField(max_length=200)
content = models.TextField(max_length=2000)
2. in your forms.py
Note: this is not necessary if you want to just use django default form. Because class-based generic views will automatically generate forms for you. However, if you need to add widget, or to add attributes (for example, add css class or id), you need to generate a customform.
from django import forms
from .models import Entry
class EntryForm(forms.ModelForm):
class Meta:
model = Entry
fields = ('title', 'content')
widgets = {
'title': forms.TextInput(attrs={'placeholder': 'Title'}),
'content': forms.TextInput(attrs={'class': 'content'}),
}
3. views.py
from .models import Entry
from django.views.generic.edit import CreateView, UpdateView
class CreateEntry(CreateView):
model=Entry
template_name = 'create_edit_entry.html' # this is the template, you might need to change its path.
form_class= EntryForm # this is added because we are using customform
success_url = '/' #this can be changed
class UpdateEntry(UpdateView):
model=Entry
template_name = 'create_edit_entry.html'
form_class= EntryForm
4. urls.py
from django.urls import path
from .views import CreateEntry, UpdateEntry
urlpatterns = [
path('entry/', CreateEntry.as_view(), name='create_entry'),
path('entry/<int:pk>', UpdateEntry.as_view(), name='update_entry'),
]
5. admins.py
from django.contrib import admin
from .models import Entry
class EntryAdmin(admin.ModelAdmin):
list_display = (('id', 'title', 'content'))
admin.site.register(Entry, EntryAdmin)
6. templates (create_edit_entry.html)
{% extends 'base.html' %}
{% block extrahead %}
{% load static %}
{% endblock %}
{% block content %}
<form action="." method="POST">
{% csrf_token %}
{{ form }}
<button type="submit">SUBMIT</button>
</form>
{% endblock %}
After you update all these files and update mysite/urls.py, you will 1) open http://127.0.0.1:8000/entry to add an entry. Check if the entry is created in your admin page. 2) then you will open http://127.0.0.1:8000/entry/1 (if the id=1) to see if your original entry is shown. 3) then you will update the form, and check if the update is successful or not in your admin.
This backbone should be able to get you started. Note that I did not put DetailView, ListView, so you need to check if the object is created and updated in your admin page. Of cause, you can add DetailView and ListView by yourself (check out django document here to learn more about generic views).
**************************************earlier answer **************
1. First thing first, it is always helpful to access form.errors when you are having trouble with forms. What you do is to add else: print(form.errors) like the following:
if form.is_valid():
# other code
else:
print(form.errors)
2.
Your edit_entry.html change to something like below: I guess you wanted use your own styling (add Title, Content etc) to the form, so you did not use {{form}}. If what I suggest worked, you can add form styling later.
{% extends "encyclopedia/layout.html" %}
{% block title %}
Edit page
{% endblock %}
{% block body %}
<form action="{% url 'edit_entry' title %}" method="POST">
{% csrf_token %}
{{form}}
</form>
{% endblock %}
3. your edit_entry view:
def edit_entry(request, title):
entry = get_object_or_404(Entry, title=title) # i assume your Model name is "Entry"
if request.method == 'POST':
form = AddForm(request.POST, instance = entry)
if form.is_valid():
print('under form.is_valid) # add this line to keep track
title = form.cleaned_data["title"]
content = form.cleaned_data["content"]
form.save()
return redirect('entry', title=entry.title)
else:
print(form.errors)
else:
form = AddForm(instance = entry)
return render(request, "encyclopedia/edit_entry.html", {
'form': form})
guys i need a small help
here is my views.py
def signup(request):
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
verification=VerificationForm(request.POST)
if form.is_valid():
userObj = form.cleaned_data
username = userObj['username']
email = userObj['email']
password = userObj['password']
return HttpResponseRedirect('/index/verification/')
# if
if not (User.objects.filter(username=username).exists() or User.objects.filter(email=email).exists()):
User.objects.create_user(username, email, password)
user = authenticate(username = username, password = password)
login(request, user)
return HttpResponseRedirect('/')
else:
raise forms.ValidationError('Looks like a username with that email or password already exists')
else:
raise forms.ValidationError('a valid')
else:
form = UserRegistrationForm()
verification = VerificationForm()
return render(request, 'question/signup.html',context= {'verification':verification,'form' : form})
here you can see i have declared two context variables one is form and the other one is verification now i want to use
now this is my signup.html
{% extends 'question/index.html '%}
{% block body_block %}
<div class="conrainer">
<form method="POST">
{% csrf_token %} {{ form.as_p }}
<button type="submit">Submit</button>
</form>
facebook auth
</div>
{% endblock %}
now i have used form variable in this page
and i want to use verification varible in another page as django views are not made to use two templates in a single view
after searching a lot in online forums i came to know about {% include tag %}
after trying to use that tag after going through documentation
this is how my verification.html is
{% include 'question/signup.html' with obj=verification only%}
<div class="container">
<form method="post">{% csrf_token %}
<p>{{verification.as_p}}</p>
<p>{{obj.as_p}}</p>
<input type="submit" name="verify" value="" action='.' align='center'name='submit'>
</form>
</div>
here is my urls.py file
urlpatterns = [
# url(r'^/',views.home,name='home'),
url(r'^home/',Home,name='home'),
url(r'^ques/',Create.as_view(success_url="/index/home/"),name='ques'),
url(r'^signup/',signup,name='signup'),
# url(r'^signup/',MyFormView.as_view(),name='signup'),
url(r'^verification/',TemplateView.as_view(template_name="question/verification.html")),
url(r'^logout/$', auth_views.logout,name='logout'),
url(r'^search/',Search,name='search'),
url(r'^accounts/', include('allauth.urls')),
# url(r'^verify/',verificationView,name='signup'),
# CreateView.as_view(model=myModel, success_url=reverse('success-url'))
]
but after trying it so many ways either obj or verification shows up in the site is there any way where i can only get the context object instead of getting the subit button and all from my signup.html using INCLUDE any kind of help is appreciated