Home > Enterprise >  Django displaying each object in a button
Django displaying each object in a button

Time:01-26

Good day, I have a page to test where tickets are created and where all created tickets (titles) are listed. Now you should be able to click on the title of a created ticket.

The button then redirects to an extra page where all the information of the specific ticket is.

I've always had trouble displaying user specific stuff, so what needs to be done so that the href="" of each link redirects in HTML to the specific ticket?

I did the following \\forms.py

  {% for i in request.user.addtitle_set.all %}
  <div>
    {% if i.user == request.user %} {{ i.title }} {% endif %}
    <form action="{% url SOMETHING_HERE i.id}" style="display: inline">
      <button type="submit">View Ticket</button>
    </form>
  </div>
  <br />
  {% endfor %}

\\urls.py

    path('dashboard/user/ticket', ticket_view),
    path('dashboard/user/ticket/<int:id>/',
         ticket_system_view, name="view_ticket"),

\\ models.py

class Ticket(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        default=None,
        null=True,
        on_delete=models.CASCADE,
    )
    title = models.CharField(max_length=200)
    description = models.TextField()
    creator_adress = models.GenericIPAddressField(null=True)
    start_date = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return str(self.title)

\\views.py

@login_required
def ticket_view(request, id, *args, **kwargs):

    try:
        obj = Ticket.objects.get(id=id)
    except Ticket.DoesNotExist:
        return redirect('/')

    if request.method == 'POST':
        form = TicketForm(request.POST, instance=Ticket(user=request.user))
        if form.is_valid():
            obj = form.save(commit=False)
            obj.creator_adress = get_client_ip(request)

            obj.save()
            return redirect('/dashboard/user/ticket')
    else:
        form = TicketForm()
    return render(request, 'ticket.html', {'form': form})


def ticket_system_view(request, id, *args, **kwargsw):
    # Here I will put all the stuff for the specific ticket page
    return render(request, 'ticket_system.html', {})

Edit: I can not even access the page because of the error TypeError at /dashboard/user/ticket

ticket_views() missing 1 required positional argument: 'id'

Edit 2.0: Solution:

    path('dashboard/user/ticket/', ticket_view),
    path('dashboard/user/ticket/<int:id>/',
         ticket_system_view, name="view_ticket"),
{% for i in request.user.ticket_set.all %}
<a href="{% url 'view_ticket' i.id %}"
  >{% if i.user == request.user %} {{ i.title }} {% endif %}</a
>
<br />
{% endfor %}

CodePudding user response:

Your urls.py

path('dashboard/user/ticket', ticket_view),
path('dashboard/user/ticket/<int:id>/',
         ticket_system_view, name="view_ticket"),

Should be

path('dashboard/user/ticket/<int:id>', ticket_view, name="some_name"),
path('dashboard/user/ticket/<int:id>/',
         ticket_system_view, name="view_ticket"),

As ticket_view expects an id

CodePudding user response:

ticket_views() missing 1 required positional argument: 'id'

In your views.py you are getting argument id so it must be passed. Change your urls.py as

path('dashboard/user/ticket/<int:pk>', ticket_view, name='ticket_view'),
path('dashboard/user/ticket/<int:id>/',
         ticket_system_view, name="view_ticket"),

Also your urlnamespace in your form as

<form action="{% url 'ticket_view' i.id %}" style="display: inline">

There must be spaces before and after % in your url template tag. You were missing ending % add that too.

  •  Tags:  
  • Related