如何将url参数传递到视图中,以供其他类函数和模板使用

如何解决如何将url参数传递到视图中,以供其他类函数和模板使用

请问我有一个严重的问题,使我连续几天都遇到各种错误。我想实现一些功能,但不知道如何实现。任何帮助将不胜感激。

我想创建一个注册表单,用于注册用户并将他们注册后发送到他们的个人资料页面。我尝试通过尝试将用户填写表单的用户名传递到我的个人资料视图的urls参数中来执行此操作。但是,我不知道该怎么做,在网上找到的每个代码只会导致我出错。我是django的新手。

除此之外,我想创建我的个人资料视图,以便如果用户搜索自己的模板,然后他们可以对其进行编辑,我为此使用了UpdateView,如果不是他们的个人资料,则他们只能以只读,我为此使用了detailView。

另外,还可以有人帮助我了解如何将url参数传递到基于类的视图中,视图中的所有函数如何访问此参数。如何在基于类的视图中的同一函数的另一个函数中使用变量。同样,使用url参数和重定向是在不同视图之间传递变量的唯一方法

请注意,我使用了自定义用户模型

这是我当前的错误

current error message

错误回溯

Traceback (most recent call last):
  File "C:\Users\Elisha\AppData\Local\Programs\Python\python38\lib\site- 
packages\django\core\handlers\exception.py",line 47,in inner
    response = get_response(request)
  File "C:\Users\Elisha\AppData\Local\Programs\Python\python38\lib\site- 
packages\django\core\handlers\base.py",line 179,in _get_response
    response = wrapped_callback(request,*callback_args,**callback_kwargs)
  File "C:\Users\Elisha\AppData\Local\Programs\Python\python38\lib\site- 
packages\django\views\generic\base.py",line 73,in view
    return self.dispatch(request,*args,**kwargs)
  File "C:\Users\Elisha\AppData\Local\Programs\Python\python38\lib\site- 
packages\django\views\generic\base.py",line 101,in dispatch
    return handler(request,**kwargs)
  File "C:\Users\Elisha\AppData\Local\Programs\Python\python38\lib\site- 
packages\django\views\generic\edit.py",line 172,in post
    return super().post(request,line 142,in post
    return self.form_valid(form)
  File "c:\MouauEasyGo\user\views.py",line 38,in form_valid
    return HttpResponseRedirect(self.get_success_url())
  File "c:\MouauEasyGo\user\views.py",line 55,in get_success_url
    return reverse('profile',args=(self.kwargs['name_of_user']))
  File "C:\Users\Elisha\AppData\Local\Programs\Python\python38\lib\site- 
packages\django\urls\base.py",line 87,in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view,prefix,**kwargs))
  File "C:\Users\Elisha\AppData\Local\Programs\Python\python38\lib\site- 
packages\django\urls\resolvers.py",line 685,in _reverse_with_prefix
    raise noreverseMatch(msg)

Exception Type: noreverseMatch at /user/register/
Exception Value: Reverse for 'profile' with arguments '('a','g','e')' not found. 1 pattern(s) tried: ['user/profile/(?P<username>[^/]+)/$']

下面是我的代码

urls.py

from django.urls import path
from user import views as user_views


urlpatterns = [
    path('profile/<str:username>/',user_views.profile_update_view.as_view(),name="profile"),path('register/',user_views.user_register_view.as_view(),name="register"),]

views.py

class user_register_view(CreateView):
    template_name = "user/register.html"
    form_class = UserRegistrationForm
    model = NewUser


    # I had to override the form_valid method and added form to the self parameter since get_success_url can't access form directly

    def form_valid(self,form):
        self.form = form
        if form.is_bound:
            self.kwargs['name_of_user'] = form.cleaned_data['user_name']
        return HttpResponseRedirect(self.get_success_url())

    def get_context_data(self,**kwargs):
        data = super().get_context_data(**kwargs)
        try:
            data['name_of_user'] = self.kwargs['name_of_user']
        except KeyError:
            return data
        return data

    def get_queryset(self):
        qs = super().get_queryset()
        return qs.filter(name__startswith=self.kwargs[self.request.user.user_name])

    # if the user's username is in slug,then use it in the profile's url else then pick the username from the form and use instead
    def get_success_url(self):
        if self.kwargs['name_of_user']:
            return reverse('profile',args=(self.kwargs['name_of_user']))


# this is the profile page as viewed by the owner of the profile
# the viewer passes the test only if they are the logged in user
# if they are not,then they are redirected to the the
# profile_detail_view.
class profile_update_view(UserPassesTestMixin,UpdateView):
    model = Profile
    fields = ['date_of_birth','country','about','image',]
    template_name = 'user/profile_update_form.html'

    def get_object(self):
        user = self.request.user
        return get_object_or_404(Profile,pk=user.id)

    # try to get the user_name from the current user.
    # if the user is an Anoynmous user then just redirect to detail page
    def test_func(self):
        try:
            x = self.request.user.user_name
            y = self.kwargs.get('name_of_user')
            if x == y:
                return True
            else:
                return redirect('profile_detail_view.as_view()')
        except AttributeError:
            return redirect('profile_detail_view.as_view()')


# this is the profile page as viewed by the general public
# this view can only be reached if the current logged  in user
# is not the one access the view
class profile_detail_view(DetailView):
    template_name = "user/profile_detail.html"
    model = Profile

    def get_object(self):
        user = self.request.user
        return get_object_or_404(Profile.objects.get(pk=user.id))

forms.py

class UserRegistrationForm(UserCreationForm):
    date_of_birth = forms.DateField(required=True)
    country = forms.CharField(max_length=50,required=True)
    image = forms.ImageField(required=False)
    about = forms.CharField(widget=forms.Textarea,required=False)

    class Meta:
        model = NewUser
        fields = ['email','user_name','first_name','last_name',]

    # save profile info as well as the user info in the form
    def save(self,commit=True):
        if not commit:
            raise NotImplementedError(
                "Can't create User and UserProfile without database save")
        user = super(UserRegistrationForm,self).save(commit=True)
        user_name = self.cleaned_data['user_name']
        email = self.cleaned_data['email']
        first_name = self.cleaned_data['first_name']
        last_name = self.cleaned_data['last_name']
        user,created = NewUser.objects.get_or_create(user_name=user_name,email=email,first_name=first_name,last_name=last_name)
        user.save()
        user_profile = Profile(
            user=user,about=self.cleaned_data['about'],date_of_birth=self.cleaned_data['date_of_birth'],country=self.cleaned_data['country']
        )
        user_profile.save()
        return user,user_profile

models.py

class CustomAccountManager(BaseUserManager):

    def create_superuser(self,email,user_name,first_name,last_name,password,**other_fields):

        other_fields.setdefault('is_staff',True)
        other_fields.setdefault('is_superuser',True)
        other_fields.setdefault('is_active',True)

        if other_fields.get('is_staff') is not True:
            raise ValueError('Superuser must be assigned to is_staff=True.')
        if other_fields.get('is_superuser') is not True:
            raise ValueError(
                'Superuser must be assigned to is_superuser=True.')

        return self.create_user(email,**other_fields)

    def create_user(self,**other_fields):

        if not email:
            raise ValueError(_('You must provide an email address'))

        email = self.normalize_email(email)
        user = self.model(email=email,user_name=user_name,last_name=last_name,**other_fields)
        user.set_password(password)
        user.save()
        return user


class NewUser(AbstractBaseUser,PermissionsMixin):

    email = models.EmailField(_('email address'),unique=True)
    user_name = models.CharField(max_length=150,unique=True)
    first_name = models.CharField(max_length=150,unique=True)
    last_name = models.CharField(max_length=150)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

    objects = CustomAccountManager()

    USERNAME_FIELD = 'email'
    required_FIELDS = ['user_name','last_name']

    def __str__(self):
        return self.user_name


class Profile(models.Model):
    user = models.OnetoOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    image = models.ImageField(default="default.jpg",upload_to="profile image/")
    about = models.TextField(_('about'),max_length=500,blank=True)
    date_of_birth = models.DateField(default=timezone.Now)
    country = models.CharField(max_length=20,blank=True,null=True)

    def __str__(self):
        return f"{self.user}'s profile"

任何输入将不胜感激。如果需要任何其他信息,请告诉我。预先感谢您的帮助。

解决方法

让我举个例子:

模板:


    <form method="GET" action="{% url 'register' %}">
        <div class="input-group">
            <input name="q" type="text" class="form-control" placeholder="Enter name..." />
            <span class="input-group-append">
                <input class="btn btn-secondary" type="submit" name="btn" value="Submit" />
            </span>
        </div>
    </form>

URL: path('register/',views.register,name='register'),

查看:

def register(request):
    name = request.GET.get('q')
    # redirect the page with this information.

实现this的参考-查看此处执行的search操作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?