added page and form for creating new assignments

parent 46dbf336
from django.forms import ModelForm, Textarea
from django.utils.translation import gettext_lazy as _
from .models import Assignment
class AssignmentForm(ModelForm):
class Meta:
model = Assignment
fields = ['name', 'description', 'max_points', 'course', 'assignment_imageUrl']
widgets = {
'name': Textarea(attrs={'cols': 60, 'rows': 1}),
'description': Textarea(attrs={'cols': 60, 'rows': 10}),
}
labels = {
'assignment_imageUrl': _('Image'),
}
...@@ -3,23 +3,30 @@ ...@@ -3,23 +3,30 @@
{% block content %} {% block content %}
<h1>New Assignment</h1> <h1>New Assignment</h1>
<hr> <hr>
<form action="./" method="post"> <form method="POST" action= "{% url 'assignments:assignment-add' %}">
{% csrf_token %} {% csrf_token %}
<label for="name">Name:</label><br> <div class="fieldWrapper">
<input type="text" name="name" id="name" style="width:400px"><br> <label for="id_name">Name:</label><br>
<label for="description">Description:</label><br> {{assignment_form.name}}
<textarea name="description" id="description" style="width:400px; height:200px;"></textarea><br> </div>
<label for="max_points">Max Points:</label><br> <div class="fieldWrapper">
<input type="number" name="max_points" id="max_points"><br> <label for="id_description">Description:</label><br>
<label for="course">Course:</label><br> {{assignment_form.description}}
<select name="course" id="course"> </div>
{% for course in course_list %} <div class="fieldWrapper">
<option value="{{course}}">{{course.course_title}}</option> <label for="id_max_points">Max Points:</label><br>
{% endfor %} {{assignment_form.max_points}}
</select><br> </div>
<label for="Image">Image:</label><br> <div class="fieldWrapper">
<input type="text" name="assignment_imageUrl" id="assignment_imageUrl"><br><br> <label for="id_course">Course:</label><br>
<input type="submit" value="Submit"> {{assignment_form.course}}
</div>
<div class="fieldWrapper">
<label for="id_description">Image:</label><br>
{{assignment_form.assignment_imageUrl}}
</div>
<br>
<button class="button" type="submit">Create Assignment</button>
</form> </form>
<h5><a href ="{% url 'assignments/index'%}">Back to Assignments</h5> <h5><a href ="{% url 'assignments:index'%}">Back to Assignments</h5>
{% endblock %} {% endblock %}
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<ul> <ul>
{% for assignment in assignments %} {% for assignment in assignments %}
{% if assignment.course.course_code == course.course_code %} {% if assignment.course.course_code == course.course_code %}
<li><a href ="{% url 'assignment-detail' assignment.id%}">{{assignment.name}}</a></li> <li><a href ="{% url 'assignments:assignment-detail' assignment.id%}">{{assignment.name}}</a></li>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
</ul> </ul>
...@@ -19,6 +19,6 @@ ...@@ -19,6 +19,6 @@
{% else %} {% else %}
<p>No courses currently enrolled.</p> <p>No courses currently enrolled.</p>
{% endif %} {% endif %}
{%comment%}<h5><a href ="{% url 'assignment-add'%}">Create New Assignment</h5>{%endcomment%} <h5><a href ="{% url 'assignments:assignment-add'%}">Create New Assignment</h5>
{% endblock %} {% endblock %}
from django.urls import path from django.urls import path
from .views import HomePageView, AssignmentDetailView from .views import HomePageView, AssignmentDetailView
from . import views
app_name = "assignments"
urlpatterns = [ urlpatterns = [
path('', HomePageView.as_view(), name='assignments/index'), path('', HomePageView.as_view(), name='index'),
path('assignment/<int:pk>/details/', AssignmentDetailView.as_view(), name='assignment-detail') path('assignment/<int:pk>/details/', AssignmentDetailView.as_view(), name='assignment-detail'),
path('add/', views.assignmentAdd, name='assignment-add')
] ]
from django.http import HttpResponse from django.http import HttpResponse
from . models import Assignment, Course from . models import Assignment, Course
from django.shortcuts import render from django.shortcuts import render, redirect
from django.views import View from django.views import View
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from .forms import AssignmentForm
# Create your views here. # Create your views here.
class HomePageView(View): class HomePageView(View):
...@@ -17,3 +18,14 @@ class HomePageView(View): ...@@ -17,3 +18,14 @@ class HomePageView(View):
class AssignmentDetailView(DetailView): class AssignmentDetailView(DetailView):
model = Assignment model = Assignment
def assignmentAdd(request):
assignment_form = AssignmentForm()
if request.method == "POST":
assignment_form = AssignmentForm(request.POST)
if assignment_form.is_valid():
assignment_form.save()
return redirect("assignments:assignment-add")
else:
assignment_form = AssignmentForm()
return render(request, "assignments/assignment_add.html", {"assignment_form":assignment_form})
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment